[SOLVED] Filling and printing 2D char array (or even array of strings) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

[SOLVED] Filling and printing 2D char array (or even array of strings)

I was trying to modify this code to allow users to choose their own characters for drawing the pattern: https://code.sololearn.com/cb9E4EXRq1Lk/?ref=app So I tried to write a piece of code that do the job: https://code.sololearn.com/c4fn2m1zNIy9/?ref=app But it doesn't work. If I input "aa bb cc dd" it prints "aa" instead of "bb" Even changing the printed subscrit to 2 or 3, it always prints "aa". And since the output is always the same, it gives me no hints about where to look for the mistake. Does anybody know what's wrong with the code? Edit: in the original code I have an array of strings rather than a 2D char array. You can run my attempt with an array of strings by deleting (or putting // to) the CHARARRAY definition. This version doesn't work at all. Either one of the two ways is fine for me.

26th Oct 2020, 11:37 AM
Davide
Davide - avatar
8 Answers
+ 1
You forget to change the token variable - just write token = strtok(NULL, " "); inside the loop
28th Oct 2020, 10:22 AM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
+ 1
Now you are missing the function name (strtok) in the same line token = strtok(NULL, " "); With that change both variants of the loop work (i < 4 and token!=NULL) But in the last case you still can only put 4 items in your array, so you should also check that you don't go over the end.
28th Oct 2020, 11:56 AM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
+ 1
For the string version it initializes each char* pointer with the address of the corresponding string constant without allocating a place for it as in the array version. Since the pointer points to a constant, trying to change it is undefined behavior. In c++ you would get an error, requiring you to have your array "const char*" . C is more permissive and just crashes. The second and third ifdefs are not necessary - when passed to the function an array decays to the pointer to its element.
28th Oct 2020, 2:58 PM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
+ 1
I meant that when you have two definitions in a function char* s1 = "my string1"; char s2[] = "my string2"; the compiler should put both variables on the stack, but for s1 it would just be one pointer (8 bytes on 64 bit system), independent of the string length, and for s2 it would be 11 chars (including the trailing zero), which correspondingly you can change inside the function. For the ifdefs i meant that if you have char s[]="hello"; and a function accepting char* void f(char* x); Thenthese two calls are equivalent: f(s); and f(&s[0]); in your case you can replace &characters[i][0] by just characters[i]
28th Oct 2020, 4:51 PM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
0
Volodymyr Chelnokov You are right! Thank you! However even fixing this, the program is still not working. Now instead of printing the wrong "aa" it prints nothing and then a warning for the fixed line saying: "left operand of comma operator has no effect" But that's the right way to write the strtok() 🤔 I am confused. edit: I changed the for() condition with: token != NULL; and now it doesn't work at all if you put again: i < 4; you'll see the warning
28th Oct 2020, 11:39 AM
Davide
Davide - avatar
0
"Now you are missing the function name" 😭 I feel so stupid 🙄 Now the program works! Thank you for the help! If you have any idea on why the array of string version doesn't work, tell me please!
28th Oct 2020, 12:43 PM
Davide
Davide - avatar
0
Thank you very much for the explanation!! I got that the strings are constant so I can't rewrite them. But what does it means that I am initializing the pointers without allocating a place for them? And what do you mean to say about the ifdef? They have a different notation, one with 2 subscripts and the other with only one.
28th Oct 2020, 4:26 PM
Davide
Davide - avatar
0
Volodymyr Chelnokov thank you so much!!! Everything crystal clear!
28th Oct 2020, 5:06 PM
Davide
Davide - avatar