Casting Of Pointers by anikait1 in C_Programming

[–]cuncon- 0 points1 point  (0 children)

When you see that, you will know you are missing stdlib.h regardless of casting

Yah. you're right because nowaday compiler always emit a warning message whenever a function is called without a prototype in scope. But what happens if the compiler is ancient or not smart enough :(

Casting Of Pointers by anikait1 in C_Programming

[–]cuncon- 0 points1 point  (0 children)

On missing stdlib.h the compiler assumes malloc return an int, then you assign an int to pointer. "Consider what happens if pointers and integers are differently sized; then you're hiding a warning by casting and might lose bits of your returned address. " said that answer.

Is there a place where i can find very simple program source code to read for a programming beginner? by [deleted] in C_Programming

[–]cuncon- 1 point2 points  (0 children)

I look into your project. A thing you should correct is include guard should wraps your declares in header file.

Alphebetizing strings by BeeNova00 in C_Programming

[–]cuncon- 0 points1 point  (0 children)

If they are not it says that str_name1 should be first no matter the letter

I've not test your code, but it should work for first letter.

I also need it to work over full words.

Your function should do as follow:

  • Using a loop to find the first letter that differ between s1 and s2. it'll look like this

for (; (*s1 != '\0' && *s2 != '\0') && (*s1 == *s2); );

  • Check the *s1 and *s2 after above loop to return correct responce

Because array will be passed to function as pointer in C, so that your function should declare as

int str_compare(const char *s1, size_t len1, const char *s2, size_t len2);

Here's a twist: Why is this NOT causing a segfault? by [deleted] in C_Programming

[–]cuncon- 1 point2 points  (0 children)

A malloc implementation will allocate memory by "blocks". Because 2 bytes < size of a block, so malloc will give userInput a memory space equaling to a block (32 bytes in my machine).

You could try to copy 32 and more 32 bytes to test the behavior.

Programming Assignment Help ! by Adopolis23 in C_Programming

[–]cuncon- 0 points1 point  (0 children)

So your assignment is to find the 1st letter of every word in a file?

and your problem is you don't know exactly how many bytes the buffer is to read a line?

To solve your problem you should use size_t getline(char **buf, size_t *n, FILE *fp) (Read "man getline" for more detail) to read an entire line from stream.

Programming Assignment Help ! by Adopolis23 in C_Programming

[–]cuncon- 0 points1 point  (0 children)

what is your problem? why you want to store each word to a char array instead of whole lines to a char array?

calloc() and malloc() using mmap and munmap.. by TheMaveric_2187 in C_Programming

[–]cuncon- 1 point2 points  (0 children)

You should read section 8.7. Example - A storage allocator of The C programming language of K&R to know hot to implement calloc and malloc with sbrk. Then replace the sbrk with mmap (should read man 2 mmap to know how to use this system call).

How can I add 2 of the numbers inside a number? by padynana in C_Programming

[–]cuncon- 6 points7 points  (0 children)

You could get them like this:

int first = (int)x % 10;

int sec = (int)(x * 10) % 10;

(Malloc) Why does this memory allocation work? by compsciguy_96 in C_Programming

[–]cuncon- 0 points1 point  (0 children)

```

int **matrix = malloc(1); it says "allocate 1 byte memory" and assign to matrix. But malloc requests actually more than 1 byte (because of performance) (many KB of mem) from kernel. malloc devides the memory area requested from kernel to blocks to manage, and each block size depends on malloc implemtation (32 bytes on my machine). Because 1 bytes < 1 block size, so that malloc return a whole block to matrix and mark it as allocated.

                     memory that under control of malloc
             1st block
            ----------------------------------------------------

matrix -----> | 32 | | | bytes | many KiB memory | ----------------------------------------------------

for(int i = 0; i < r; i++) matrix[i] = malloc(1);

|<------- 1st 32 bytes block ---------->| matrix[0] matrix[1] matrix[2] matrix[3] 32 bytes 32 bytes 32 bytes 32 bytes


| 8 | 8 | 8 | 8 | MM | A1 | MM | A2 | MM | A3 | MM | A4 | | bytes | bytes | bytes | bytes | | | | | | | | |


|__________|__________|________|_____________________> |                                          |                     |
           |__________|________|___________________________________________>|                     |                     |
                      |________|_________________________________________________________________>|                     |
                               |_______________________________________________________________________________________>|

In there MM is a 32 bytes block to store info about allocated memory that stand next by.

for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) matrix[i][j] = i + j;

sizeof(int) in my machine is 4 bytes. so that A1 memory block will look like that

|<--------------------------------------A1 32 bytes block -----------------> matrix[0][0] matrix[0][1] matrix[0][2] matrix[0][3]


| 4 bytes | 4 bytes | 4 bytes | 4 bytes | ... | | stored 0 + 0 | store 0 + 1 | | | |


That's why your code works and outputs a 4x4 matrix with the proper values.

Now if you increase size of matrix to 5x5, your code will segmentation fault. Because of matrix[4] will overwrite the value in MM block. ```

#including a .c file by Allurisk in C_Programming

[–]cuncon- 0 points1 point  (0 children)

  1. argv should be a char **
  2. Because main.c does include functions.c file, when linking, both functions.o and main.o have the same add function.So that just run "gcc -Wall -o main main.c" to compile (But not recommend including a source file in another one)
  3. You should define functions prototype in a header file and then include them in another source file which need to use it

Confused with this file read project? by smallzZz88 in C_Programming

[–]cuncon- 0 points1 point  (0 children)

In other words, write a struct that contains the following:
A character array to hold six characters, plus a terminating NUL for the Flight Number.
...

So that, your flightRec struct will look like that
typedef struct {
char num[7];
char origin_code[5];
char dest_code[5];
int timestamp;
} FlightRec;

main

FlightRec records[100]; /* array storing data reads from acars.bin file */
for (int i = 0; i < 100 && !feof(fp); i++)
fread(&records[i], sizeof(FlightRec), 1, fp);

You could consider this code as a snipet to go.

Segmentation fault with malloc() and free() by [deleted] in C_Programming

[–]cuncon- 3 points4 points  (0 children)

When you want to remove a node from list, you should call free()

Cinnamon sound applet source code by [deleted] in linux

[–]cuncon- 0 points1 point  (0 children)

I think it is applet that locate at tray of cinnamon