How can I change a character of a string within an array of strings?
An execution error happens if I run the code below..

int main(int argc, char *argv[])
{
  char *d[] = {"aaaa","bbbb","cccc"};

  cout << d[2] << endl; //PRINT cccc
  d[2][2] = 'x';
  cout << d[2] << endl; // IT SHOULD BE PRINTED ccxc BUT AN EXECUTION ERROS HAPPENS
}

Grato

Did this even compile? I ask because I had to add
#include <iostream.h>
at the top to get a compile, at which point it ran OK.

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.

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 wouldn't want to do that, I think it's undefined behaviour when you try to do that. Why can't you create your array of char arrays like Aia said?

char d[][5]

As I mentioned, this code worked fine for me as written once I included the iostream library. However, as per Aia's comment, this might be because I'm using the Watcom C++ compiler - perhaps there are inane subtleties because of compiler flavor?

Anyway, I noticed this thread - http://www.velocityreviews.com/forums/t286914-array-of-strings.html - which addresses the problem in C++ using vectors which might be a better way to do it though it's impossible to tell without knowing more about the larger problem.

iostream isn't a C library? afaik it's stdio.h.

C++ has different rules regarding this issue apparently.

Use strcpy(string_to_be_copied, string_with_values_to_be_copied) to copy one string to another.

But don't do that on those strings. Just accept that they are read-only, you are *not* allowed to write to them, end of story.

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".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.