Basic stuff, changing values in a static const char
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
Compiling all good
testarray[0] = name works but give bad characters
strcpy(testarray[0],name); crashes at runtime
testarray[0] = name works but give bad characters
strcpy(testarray[0],name); crashes at runtime
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
i.e.
char* myStr = "Hello World";
myStr[3] = 'f'; // illegal
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.
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.
Quote:Original post by ReeganNope, it's an array of char pointers, which is fine.
Im no pro but shouldnt "static const char* testarray[]" be a 2d dimensional array?
Quote:Original post by MarktheguyThis line sets the first pointer in testarray to point to the name buffer:
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
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).
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
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
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...
Quote:Original post by MikeTacularQuote: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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement