Hi all,
Can somebody help me pls.
I need to create an array of strings using a set of string pointers.

char *name1 = "aa";
char *name2 = "bb";

want to make an array like
char *nameall[] = {name1, name2};
But this does not work :(

Is there a simple way to make the array in a simple way than assigning for each element.
I mean without doing
name[0] = name1;
name[1] = name2;

Recommended Answers

All 6 Replies

>I need to create an array of strings using a set of string pointers.
Why don't you describe what you're doing rather than how you want to do it. Perhaps there's a better way.

Actually what I want to do is to make a string pointer array (2D) from set of string pointers.

suppose I have set of string pointers *name1, *name2, *name3.

I want to make a single array from this set of pointers. Like

name_all[0] = name1
name_all[1] = name2 etc kind array

>Actually what I want to do is to make a string pointer array (2D) from set of string pointers.
Congratulations, you completely failed to do as I requested by repeating your original question with different words.

I'm willing to bet that you'd be better off with a smarter data structure than an array (though you already have your solution for the array), but until you describe what you're trying to accomplish by collecting these names, I can't say for sure.

These set of strings name1, name2, etc are set of strings gathered from different functions. Finally I will have to use them at once so the option was to make a array of strings through them. So after that when I went for assigning elements what happeand was My boss looked at the code and asked me two remove one by one assignment and asked me do a single line assignment for easiness of the code. So Now Im looking for that kind of assignment but nothings working :((

>These set of strings name1, name2, etc are set of strings gathered from different functions.
Why aren't you gathering them into a 2D array to begin with?

>My boss looked at the code and asked me two remove one by one assignment
>and asked me do a single line assignment for easiness of the code.
You mean for conciseness. Easy and short are quite often contradictory goals. In this case, you're trying to do something different (initialization instead of assignment) and your compiler is rightly complaining. You can easily put the assignments on one line:

name_all[0] = name1; name_all[1] = name2

And even in one statement:

name_all[0] = name1, name_all[1] = name2

But that doesn't make the code any easinessier. It just makes it fuglier and ultimately more difficult to grok.

My recommendation is to use the data structure you want from the beginning rather than try to shoehorn it in after the fact.

Were I you, I'd also tell the boss that if he wants to write this code, he's welcome to it, but if he doesn't, then he'll just have to accept that I'm a good enough programmer not to need a backseat programmer squawking in my ear about trivial things.

Thanx for ur support. I decided to go for the old fashined one by one assigning.
Seems like no other shorter way of doing the thing.

For anybody interested .....
What I did was

Have few strings *name1, *name *name3 etc.

initialize a char *name[num_of_strings]

then
name[0] = name1;
name[1] = name2; etc

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.