Jump to content
  • Advertisement
Sign in to follow this  
Marktheguy

Basic stuff, changing values in a static const char

This topic is 3438 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

static const char *testarray[] = {"test1","test2","test3", NULL}; char test[30]; strcpy(name,"testing"); testarray[0] = name;//not working strcpy(testarray[0],name);//not working Any help? Using C

Share this post


Link to post
Share on other sites
Advertisement
How is name defined? And do you have enough memory allocated to store the strings in the variables? Also, you can't write to a string literal, so strcpy(testarra[0], name) should fail.

i.e.

char* myStr = "Hello World";
myStr[3] = 'f'; // illegal

Share this post


Link to post
Share on other sites
Im no pro but shouldnt "static const char* testarray[]" be a 2d dimensional array?

swap this line:
static const char* testarray[] = {"test1","test2","test3", NULL};

with this line:
static const char* testarray[4][] = {"test1","test2","test3", NULL};

*not tested*
maybe something like this. sorry if im wrong >.<

EDIT: Tested it, and its wrong.. i now see why.

Share this post


Link to post
Share on other sites
Quote:
Original post by Reegan
Im no pro but shouldnt "static const char* testarray[]" be a 2d dimensional array?
Nope, it's an array of char pointers, which is fine.

Quote:
Original post by Marktheguy
static const char *testarray[] = {"test1","test2","test3", NULL};
char test[30];


strcpy(name,"testing");
testarray[0] = name;//not working
strcpy(testarray[0],name);//not working
This line sets the first pointer in testarray to point to the name buffer:
testarray[0] = name;
That'll be fine, until name goes out of scope or is filled with different data.

This line writes into the buffer pointed at by testarray[0]:
strcpy(testarray[0],name);
That'll be fine if you've executed the line before it, since it points at the name buffer (Although it's a no-op, you're copying name into name). If not, then you're overwriting the "test1" string, which is a string literal, and is constant. You can't do that.

You probably want to dynamically allocate memory for the strings - or, better, use C++ which has std::string (Although I appreciate you might not be able to use C++ if this is homework).

Share this post


Link to post
Share on other sites
Thanks for all you help

It's only C, sad but true..

Tried
strcpy(mystring,"testing");
myarray[0] = mystring;
strcpy(myarray[0],mystring);

But it ends up with bad characters.
There's plenty of space in the static const char myarray

Share this post


Link to post
Share on other sites
Quote:
There's plenty of space in the static const char myarray

How much space? How is myarray defined? If you define it as a static const char, then there definitely isn't enough room.

And this:

myarray[0] = mystring;
strcpy(myarray[0],mystring);

actually doesn't do much. You set myarray[0] to point to the same memory location that mystring points to, so they both point to the same memory. Then you try to copy that memory to itself, so it ends up not changing anything...

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
Quote:
There's plenty of space in the static const char myarray

How much space? How is myarray defined? If you define it as a static const char, then there definitely isn't enough room.

And this:

myarray[0] = mystring;
strcpy(myarray[0],mystring);

actually doesn't do much. You set myarray[0] to point to the same memory location that mystring points to, so they both point to the same memory. Then you try to copy that memory to itself, so it ends up not changing anything...


static const char *myarray[] = {"EmptyEmpty","EmptyEmpty","EmptyEmpty", NULL};

But I dont get it, if they point to the same memory shouldt myarray[0] get the exact same valuestring as mystring?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!