LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-13-2006, 12:21 PM   #1
anamericanjoe
Member
 
Registered: May 2006
Posts: 69

Rep: Reputation: 16
Array of Pointers to C Strings


Let's say I initialize an array of points to C strings:

Code:
char* array[] = {"one", "two", "three", "etc"};
Is there a way to initialize an empty array of char pointers and then add in char pointers later on?

In other words, how would you accomplish something like this:

Code:
char* array[4];
char* one = "one";
array[0] = &one;
char* two = "two";
array[1] = &two;
char* three = "three";
array[2] = &three;
char* four = "four";
array[3] = &four;
I know that the code above does not work, but I'm not sure why it does not.

I do know that the above procedure should work when dealing with ints:

Code:
int* array[4];
int one = 1;
array[0] = &one;
int two = 2;
array[1] = &two;
int three = 3;
array[2] = &three;
int four = 4;
array[3] = &four;
 
Old 12-13-2006, 01:10 PM   #2
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
Code:
char* array[4];
char* one = "one";
array[0] = one;
char* two = "two";
array[1] = two;
char* three = "three";
array[2] = three;
char* four = "four";
array[3] = four;
Just remove the "&".
It didn't work because you assigned to a pointer that stored a char adress an adress of a pointer to char.

Last edited by BiThian; 12-13-2006 at 01:16 PM.
 
Old 12-13-2006, 01:13 PM   #3
anamericanjoe
Member
 
Registered: May 2006
Posts: 69

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by BiThian
Code:
char* array[4];
char* one = "one";
array[0] = one;
char* two = "two";
array[1] = two;
char* three = "three";
array[2] = three;
char* four = "four";
array[3] = four;
Just remove the "&".
There are no & in the example with char. Were you referring to the int example?
 
Old 12-13-2006, 01:16 PM   #4
anamericanjoe
Member
 
Registered: May 2006
Posts: 69

Original Poster
Rep: Reputation: 16
Same problem put another way

To put the problem another way, I would like to know how to build an equivalent structure to

Code:
char* array[] = {"one", "two", "three","etc"};
without the benefit of having the strings available when I initialize the array of array of char pointers.
 
Old 12-13-2006, 01:17 PM   #5
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
In your example, there are "&".
LE: I just told you how to do it in my first post(and that without any initialization).

Last edited by BiThian; 12-13-2006 at 01:25 PM.
 
Old 12-13-2006, 01:28 PM   #6
anamericanjoe
Member
 
Registered: May 2006
Posts: 69

Original Poster
Rep: Reputation: 16
Question New related question

BiThian, you're right--I misread my original post.

So, that introduces a new question.

When I have
Code:
char* array[] = {"one", "two", "three",(char *) 0};
for(int i = 0; i < 3; i++){                                                                                           
     char* temp = array[i];                                                                                        
     cout << temp << endl;                                                                                            
}
I get the output:

Quote:
one
two
three
But when I build the array myself:

Code:
char* array[4];
char* one = "one";
array[0] = one;
char* two = "two";
array[1] = two;
char* three = "three";
array[2] = three;
char* four = (char *) 0;
array[3] = four;

for(int i = 0; i < 3; i++){                                                                                           
     char* temp = array[i];                                                                                        
     cout << temp << endl;                                                                                            
}
I get:

Quote:
o
t
t
Why is there a difference?

Last edited by anamericanjoe; 12-13-2006 at 01:30 PM.
 
Old 12-13-2006, 01:35 PM   #7
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
Hmm....I get the same output in both cases.

Last edited by BiThian; 12-13-2006 at 01:58 PM.
 
Old 12-13-2006, 06:44 PM   #8
anamericanjoe
Member
 
Registered: May 2006
Posts: 69

Original Poster
Rep: Reputation: 16
Smile Thanks!

Thanks for your patience, BiThian. I finally got things to work as they should.

I'm not sure if this was contributing to my initial problem, but I don't think I was adding a null terminator to the end of my array. Maybe that's why things weren't printing out quite right? At this point, I'm just glad that things work.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c array of pointers greg108 Programming 10 08-17-2005 10:25 AM
Initializing Array of Strings jrtayloriv Programming 10 02-03-2005 12:22 PM
pointers and strings skibud2 Programming 4 09-22-2003 07:38 AM
Join an array of strings with C? notsoevil Programming 3 06-20-2003 10:58 AM
C: pointers, strings, splitting headache notsoevil Programming 5 06-19-2003 04:04 PM


All times are GMT -5. The time now is 03:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration