Array of string

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 5
Reputation: dusse is an unknown quantity at this point 
Solved Threads: 0
dusse dusse is offline Offline
Newbie Poster

Array of string

 
0
  #1
Oct 15th, 2008
How can I change a character of a string within an array of strings?
An execution error happens if I run the code below..
  1. int main(int argc, char *argv[])
  2. {
  3. char *d[] = {"aaaa","bbbb","cccc"};
  4.  
  5. cout << d[2] << endl; //PRINT cccc
  6. d[2][2] = 'x';
  7. cout << d[2] << endl; // IT SHOULD BE PRINTED ccxc BUT AN EXECUTION ERROS HAPPENS
  8. }
Grato
Last edited by Narue; Oct 16th, 2008 at 2:35 pm. Reason: added code tags and formatting
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: DevonMcC++ is an unknown quantity at this point 
Solved Threads: 1
DevonMcC++ DevonMcC++ is offline Offline
Newbie Poster

Re: Array of string

 
0
  #2
Oct 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Array of string

 
0
  #3
Oct 15th, 2008
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.
Last edited by Aia; Oct 15th, 2008 at 8:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: dusse is an unknown quantity at this point 
Solved Threads: 0
dusse dusse is offline Offline
Newbie Poster

Re: Array of string

 
0
  #4
Oct 16th, 2008
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 ?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Array of string

 
0
  #5
Oct 16th, 2008
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?

  1. char d[][5]
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: DevonMcC++ is an unknown quantity at this point 
Solved Threads: 1
DevonMcC++ DevonMcC++ is offline Offline
Newbie Poster

Re: Array of string

 
0
  #6
Oct 16th, 2008
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/forum...f-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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Array of string

 
0
  #7
Oct 16th, 2008
iostream isn't a C library? afaik it's stdio.h.

C++ has different rules regarding this issue apparently.
Last edited by Clockowl; Oct 16th, 2008 at 1:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Array of string

 
0
  #8
Oct 16th, 2008
Use strcpy(string_to_be_copied, string_with_values_to_be_copied) to copy one string to another.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Array of string

 
0
  #9
Oct 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Array of string

 
0
  #10
Oct 16th, 2008
Originally Posted by dusse View Post
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".
Last edited by Aia; Oct 16th, 2008 at 7:21 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC