char *d[] = {"aaaa","bbbb","cccc"};
These strings reside in read only memory. They can not be altered. You can not re-assign to them, without raising an error.
d[2][2] = 'x';
Wrong!
>How can I change a character of a string within an array of strings?
char *d[] is an array of pointers to string.
What you want is char d[][5]
Your code is not C but C++, there's a different forum for C++, since they are not the same language.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
Thanks for your reply Aia,
sorry I know that this code is C++, but my problem is also in C !!
char *d[] = {"aaaa","bbbb","cccc"};
you said that these strings cannot be altered
but if we do something like:
d[2]="xxxx";
it will not raise an error
there is no way to alter a character in a array of pointers to string ?
You are confusing pointers with array of chars. char *d[] = {"aaaa","bbbb","cccc"}; is an array of pointer. Individually represented would be as:
d[0] = "aaaa";
d[1] = "bbbb";
d[2] = "cccc";
They are pointing to some piece of memory that you can't change due to how you declared it initially.
Any of those subscripts holds the address of where that piece of memory is. Which memory you can not re-assign to it. d[2] = "xxxx"; is just changing where the pointer (variable d[2]) is pointing to. Not the content. That's possible thanks to the way you declared it and initialized it. Now it is pointing to another place, by virtue of holding the address of the beginning of that memory, which contains "xxxx"
However you just left behind a piece of memory containing 'c''c''c''c''\0' and there's no way to access to it anymore. Commonly named "memory leak".
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218