Jump to content

[SOLVED] C++ Multidimensional Array Issues


Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/173692-solved-c-multidimensional-array-issues/
Share on other sites

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?

Here is my code & error. It is for a cootie game for class :P

 

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.

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.