[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 678
  • Last Modified:

set value of a char **

char *str[2] = {"abc","xyz"};
printf("str[0] = %s, str[1] = %s\n", str[0], str[1]);

//try to change abc to aXc by setting character index;
printf("[0]:%c , [1]:%c, [2]:%c\n", str[0][0], str[0][1], str[0][2]);//before

str[0][1] = 'X';// ===>  why doesn't this work

printf("[0]:%c , [1]:%c, [2]:%c\n", str[0][0], str[0][1], str[0][2]);//after

...this doesn't work ...?why?
0
cafechris
Asked:
cafechris
2 Solutions
 
imladrisCommented:
Syntactically this will work. However, str[0] is pointing to a string in the "literal pool". This is a section of memory dedicated to providing such "preinitialized" strings for the programs use.

If your compiler protects this section of memory (as mine does) then you the program will halt (in one fashion or another) when it tries to modify that memory.
The reason is to avoid obscure bugs, such as would be caused by the alteration of the literal in something like:

printf("This is a test string %d\n",i);

The solution is to create an "empty" string, and copy to it.

char alter[5];
strcpy(alter,str[0]);
alter[1]='X';

will work fine.
0
 
cafechrisAuthor Commented:
Maybe it was my project. It seemed to work fine when I made a new project and coppied my code in.
Using MSVC++ 6.

then when I add your code, it doesn't replace the character.???

Wondering why it works now, but would crash earlier today...

I will look into it
0
 
twobitadderCommented:
char str[2][4] = {"abc","xyz"};

will make the character arrays mutable, the problem being exactly as imladris mentioned about immutable string literals in read only memory, which is why you can't modify one of them to the character 'X' unless you use an array.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
twobitadderCommented:
You allow 4 spaces to fit in the terminating null \0 that represents the end of the character sequence.
0
 
PaulCaswellCommented:
>>Wondering why it works now, but would crash earlier today...
I'd guess you had string pooling switched on (/Gf) in one project and off in the other.

Paul
0
 
cafechrisAuthor Commented:
>>I'd guess you had string pooling switched on (/Gf) in one project and off in the other.
Not unless it somehow does it automatically. The only thing differenct about the 2 projects, was some insertion of assembly code in the one that would crash. My VC++ Kept locking up on me when I opened that workspace/project. But the strange thing is that a friend of mine who brought the question to my attention, couldn't get it to work without crashing either. Well, I am going to try to figure out more, it isn't worth too much time, but it is curious.

Thanks for your response,

Chris
0
 
cafechrisAuthor Commented:
Sorry guys, I totally forgot about this.
0
 
cafechrisAuthor Commented:
What the heck, I just gave the accepted answer to imladris ....I don't understand what happened. I will let community support know.
Sorry, I really made a mess of this question.

Thanks for your help guys.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now