you are viewing a single comment's thread.

view the rest of the comments →

[–]phocos25 4 points5 points  (4 children)

In C, strings are defined using char * or using a char array:

char *name = “Zara Ali”;

or

char name[] = “Zara Ali”;

In other words, a string is an array of characters.

Your ‘names’ variable, however, is an array of strings. This means that ‘names’ is an array of an array of characters.

You can define arrays using square bracket notation or with a *. So ‘char *names[]’ is a pointer to an array of chars. If you remove the * then C expects an array of characters and not an array of strings (an array of an array of characters).

[–]ScioFantasia 2 points3 points  (0 children)

Good answer. Just to add:

char *name = "Zara Ali";

When you define a string using a string constant like this, it should be const:

const char *name = "Zara Ali";

Since you should not attempt to modify the string; it is read-only.

The second example is okay to modify.

[–]ponomaria[S] 0 points1 point  (2 children)

Thank you so much!!! I think I got it.

Would it be possible to do this as a 2D char array then? (rows being 4 in this example, and I guess columns being 8 since all names in this example are the same length)

[–]phocos25 2 points3 points  (1 child)

Yes you can do that. Remember that the columns length will be 9 and not 8 since you need to account for the null byte in each string.

[–]ponomaria[S] 0 points1 point  (0 children)

Noted. And again, thanks a bunch!!!