seventheyejosh Posted September 9, 2009 Share Posted September 9, 2009 Hello All! I've begun learning C++ for college, and my php knowledge has made it very smooth. However, I cannot get array assignments to work . I believe I'm misunderstanding pointers/references, or not using strcpy(). Not sure what tho: char something[2][50]; something[0]="Yo"; something[1]="Ho"; something[2]="What Is This"; yields : incompatible types in assignment of 'const char[LENGTH OF STRING]' to 'char [5]' where length of string is 2,2, and 12 in this case. Thanks all! This is very confusing to me. Quote Link to comment https://forums.phpfreaks.com/topic/173692-solved-c-multidimensional-array-issues/ Share on other sites More sharing options...
KevinM1 Posted September 9, 2009 Share Posted September 9, 2009 Hello All! I've begun learning C++ for college, and my php knowledge has made it very smooth. However, I cannot get array assignments to work . I believe I'm misunderstanding pointers/references, or not using strcpy(). Not sure what tho: char something[2][50]; something[0]="Yo"; something[1]="Ho"; something[2]="What Is This"; yields : incompatible types in assignment of 'const char[LENGTH OF STRING]' to 'char [5]' where length of string is 2,2, and 12 in this case. Thanks all! This is very confusing to me. Well, first, you're going outside of your array boundaries. something[2][50] is actually something[0-1][0-49]. Just like in PHP, arrays are 0-indexed. The number of elements you specify are indexed as num-1. Also, remember that C-style strings are null-terminated, so you have to account for that as well. I'm wondering where the char[5] part of the error is coming from. Can you show more code? Quote Link to comment https://forums.phpfreaks.com/topic/173692-solved-c-multidimensional-array-issues/#findComment-915617 Share on other sites More sharing options...
seventheyejosh Posted September 9, 2009 Author Share Posted September 9, 2009 Here is my code & error. It is for a cootie game for class char parts[6][50]; parts[0]="add the body"; parts[1]="add the head"; parts[2]="add an antenna, hat, or bow"; parts[3]="add an eye"; parts[4]="add a tongue, teeth, or lips"; parts[5]="add a leg"; and the error cootie.cpp: In function ‘int main()’: cootie.cpp:13: error: incompatible types in assignment of ‘const char [13]’ to ‘char [50]’ cootie.cpp:14: error: incompatible types in assignment of ‘const char [13]’ to ‘char [50]’ cootie.cpp:15: error: incompatible types in assignment of ‘const char [28]’ to ‘char [50]’ cootie.cpp:16: error: incompatible types in assignment of ‘const char [11]’ to ‘char [50]’ cootie.cpp:17: error: incompatible types in assignment of ‘const char [29]’ to ‘char [50]’ cootie.cpp:18: error: incompatible types in assignment of ‘const char [10]’ to ‘char [50]’ i believe the const char [length] is refering to the rvalue. Quote Link to comment https://forums.phpfreaks.com/topic/173692-solved-c-multidimensional-array-issues/#findComment-915652 Share on other sites More sharing options...
corbin Posted September 9, 2009 Share Posted September 9, 2009 The difference is that the thing being assigned is a const char, and the array thing is a char array. To assign something to a char array like that, the lengths will need to match. Or, instead of an array, you could use a pointer. char* parts[6]; parts[0]="add the body"; parts[1]="add the head"; parts[2]="add an antenna, hat, or bow"; parts[3]="add an eye"; parts[4]="add a tongue, teeth, or lips"; parts[5]="add a leg"; Depending on how you plan on manipulating the array though, the pointer way might not be the best idea. Quote Link to comment https://forums.phpfreaks.com/topic/173692-solved-c-multidimensional-array-issues/#findComment-915685 Share on other sites More sharing options...
seventheyejosh Posted September 9, 2009 Author Share Posted September 9, 2009 Thx, that worked awesome Quote Link to comment https://forums.phpfreaks.com/topic/173692-solved-c-multidimensional-array-issues/#findComment-915749 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.