0
Why segmentation fault
3 Answers
+ 1
The 'buff' pointer in main() is not assigned to any memory buffer where your characters can go, it's just pointing at an undefined location. Either make it a static array
char buff[50];
or allocate some memory for your array (don't forget to free)
0
https://code.sololearn.com/cbQ8g2jlbu95/?ref=app
Why is this happen
gcc argv_to_string.c -o argv
argv_to_string.c:11:19: warning: sizeof on array function parameter will return size of 'char **' instead of 'char *[]'
int n = sizeof(argv) / sizeof(argv[0]);
^
argv_to_string.c:9:27: note: declared here int main (int argc, char *argv[]) {
0
Jables
Arrays in C are pointers - they point to the first element in the array
When you do arr[i], it is the same as writing *(arr + i).
So whenever arrays are to passed to functions, they 'decay' to pointers. In main(), argv is an array recieved as argument, but you can't use it as a normal array since has decayed into a pointer. So when you do
sizeof(argv)
you are basically getting the size of a pointer (char** in this case)
So the above expression is now equivalent to
sizeof(char**) / sizeof(char**)
which will always be 1
This is what the compiler is warning you about.
If it sounded confusing to you, it's ok, because it seems ridiculous at first. You can read the answers on this thread, maybe they'll be clearer
https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay
You don't need the 'n' variable anyways. The 'argc' argument stores the length of the 'argv' array, just use that.