954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array of string

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

dusse
Newbie Poster
5 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

DevonMcC++
Newbie Poster
16 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

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 ?

dusse
Newbie Poster
5 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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]
Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

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.

DevonMcC++
Newbie Poster
16 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

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

C++ has different rules regarding this issue apparently.

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

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

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

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.

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You