• C

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?
LVL 1
chris rrrsoftware/pipelineAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chris rrrsoftware/pipelineAuthor 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
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

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
chris rrrsoftware/pipelineAuthor 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
chris rrrsoftware/pipelineAuthor Commented:
Sorry guys, I totally forgot about this.
0
chris rrrsoftware/pipelineAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.