0

Hi, I am sure this question must have been asked before but I can't find it anyway.

I have initialised an array of strings

char names[][90]

I don't understand why

char names[2] = "Bert"

doesn't work (incompatible assignment error), even so I have tried

dir[2][90] = "Bert" and strcpy("Bert", dir[2])

but the latter just crashes the program.


On a slightly related topic I was also wondering why you can use terminate for loops of a string like this

char name[] = "Bert";

for(int i =0; name[i]; i++)

Yet if I put "Be0rt" it doesn't terminate when it sees the zero only the null terminator. Is this because 0 doesn't have the value of zero in ASII or is there another reason?

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by nmaillet
0

For your last question, yes it is because the character 0 has a value of 32 I think. The character with the value of 0, or NULL, is '\0'.

For the first part, char names[][90] , would not work because it would 0 char arrays of size 90. As for char names[2]="Bert" , you are trying to create a new variable with the same name. Try using names[2] = new char[90] or look at the malloc function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.