If in short - use the same prototype for declaring a variable and for parameters in a function and for strData. This is not the most beautiful option, but it will work.
Here is what your program will look like:
#include <stdio.h> #include <string.h> void CreateCSV(char str[33][40]) { char strData[33][40]= {"\"","D","A","T","A","\"",";","\0"}; if(str != NULL) memcpy (str, strData, sizeof(strData)); else printf ("error\n"); } int main (void) { char str[33][40] = {'\0'}; int i=0; CreateCSV(str); for (i=0; i<33; i++) printf ("%s\n", str[i]); return 0; }
Pay attention to the following. nuances - I used the copy memory function and not the memcpy strings. And checked on the NULL name str without specifying the shift. This is due to the way the C language represents and processes multidimensional arrays. Also, when filling in, strData used double quotes, otherwise the compiler put the letters together in one line, and did not treat them as a series of lines with 1 letter in each line.
If in the details and features of the physics of the C language: Arrays declared as
char str[33][40];
And How
char str**;
these are completely different things.
char str [33] [40]; - interpreted as a long one-dimensional array where all the lines are stuck together one after another. Something in the form "line 1 \ 0 \ 0 \ 0 \ 0 \ 0 ... \ 0 line 2 \ 0 \ 0 \ 0 \ 0 \ 0 ... \ 0 ....... line 33 \ 0 \ 0 \ 0 \ 0 \ 0 ... \ 0 " and the size of such an array will be raver 33 * 40 * sizeof (char) = 33 * 40 * 1 = 1320.
When declaring such an array, the compiler allocates 1320 bytes in the stack, and treats the variable 'str' as a pointer to this long string.
char str ; ** is an array of pointers to one-dimensional strings. Perceived by the compiler as {* pointer_to_str1, * pointer_to_str2, ... * pointer_to_str33}, while the lines themselves are outside of this array. The size of such an array will be 33 * sizeof (char *) = 33 * 4 = 132 /
When declaring such an array, the compiler performs several operations. First, the compiler allocates 33 independent lines in the stack, then creates an array of pointers and fills it with pointers to these 33 independent lines.
Here is what your program will look like if strData is declared as char **:
#include <stdio.h> #include <string.h> void CreateCSV(char str[33][40]) { char *strData[33] = {"\"","D","A","T","A","\"",";","\0"}; if(str != NULL) { int i=0; for (i=0; i<33; i++) if (strData[i] != NULL) strncpy (str[i], strData[i], 40); } else printf ("error\n"); } int main (void) { char str[33][40] = {'\0'}; int i=0; CreateCSV(str); for (i=0; i<33; i++) printf ("%s\n", str[i]); return 0; }
PS both examples are checked for gcc