I receive this error message:
"error C2440: '=' : cannot convert from 'char' to 'char [50]'"

when this statement:
name_sel = *name [ name_int ];

or this statement:
name_sel = name [ name_int ];

is compiled. The context for this is the declaration of an array of names (character strings) for which I attempt to have the user input an integer to correspond to a selected name, and then to have that name retrieved from the array and used as a character string further down in the program.

Following are further code elements:

#define STRLEN 50

Char	*name[] =
	 	{
	  	"ANGIE",
		"BEVERLY",
		"CARLOTTA",
		"DANIELLE",
		"EVELYN"
		};

Char	name_num [STRLEN];
Char	name_sel   [STRLEN];
Int	name_int;
Int            i;

Selected Name: ", STRLEN, name_num );

printf ( "Name_number = %s\n", name_num );

name_int = atoi( name_num );

printf ( "name_int: %i\n", name_int);

name_sel = *name [ name_int ];

for ( i=0; i<5; i++)
	{
	printf ( "The Friend's Name is: %s\n", 	name[i]);
	}

printf ( "Name_sel = %s\n", name_sel );

Your help is appreciated.

line 17: doesn't make any sense. You might as well just delete that line.

line 25: you can't copy character arrays like that. Either (1) make name_sel a pointer, then line 25 will work, or (2) call strcpy() to copy the two strings.

Selected Name: ", STRLEN, name_num );

this line dint make much sense to me. :|

anyways..talking about your problem..

name_sel = *name [ name_int ];

this wont work at all. You are assigning a character to a character array. that seems illogical.
*name[name_int] actually yields the first character of the name_int(th) string (name here) in the array.

as ancient dragon said..you will have to make use of a pointer judiciously...
else you may use strcpy()...that would be an easy solution..if you dont want to take the headache of pointers.. :)
gud luck... :)

I would also prefer that you use c++ based console in and out mechanisms.

printf ()

You should consider using

cout<<

line 17: doesn't make any sense. You might as well just delete that line.

line 25: you can't copy character arrays like that. Either (1) make name_sel a pointer, then line 25 will work, or (2) call strcpy() to copy the two strings.

Thank you for this response. This was my first post (ever), so please bear with me as I try to comply with posting rules and regs. In as much as the information I originally posted was lifted out of the context of my program, it was not intended to represent a fully compilable segment of code. Rather, it was intended to focus on the specific problem I'm currently having, for which I suspect the second part of your response will be applicable.

Line 17 was an incomplete statement to indicate a user interaction whereby I solicit for a response in the form of an integer, 1 through 5.

Now I'm trying to understand the proper syntax for using the integer response as a pointer to the corresponding character string contained within the "name" array, and then hand off that character string (variable length) to a subroutine (not present here) further down the line in the program. I'm not there yet, but I'm hopeful. Thanks.

Selected Name: ", STRLEN, name_num );

this line dint make much sense to me. :|

anyways..talking about your problem..

name_sel = *name [ name_int ];

this wont work at all. You are assigning a character to a character array. that seems illogical.
*name[name_int] actually yields the first character of the name_int(th) string (name here) in the array.

as ancient dragon said..you will have to make use of a pointer judiciously...
else you may use strcpy()...that would be an easy solution..if you dont want to take the headache of pointers.. :)
gud luck... :)

Thanks, Bhoot,

I think I could reduce my pointer headaches if only I could understand pointers better.

I'm trying to think about this conceptually. Somewhere in memory I've declared a character array (not an array of individual characters, but an array of variable length strings made up of characters) and I want to point to a given element of that array, i.e., to one of those strings, name[0] through name[4], and then extract that character string and place it in a character string variable.

Intuitively I know this is possible. I'm just not getting it. But thanks again for your observations.

Thanks, Bhoot,

I think I could reduce my pointer headaches if only I could understand pointers better.

I'm trying to think about this conceptually. Somewhere in memory I've declared a character array (not an array of individual characters, but an array of variable length strings made up of characters) and I want to point to a given element of that array, i.e., to one of those strings, name[0] through name[4], and then extract that character string and place it in a character string variable.

Intuitively I know this is possible. I'm just not getting it. But thanks again for your observations.

hmm..OK. Let me help you with some explanations.
I am sure you know that basically a pointer points to some variable. and int* will point to an integer.
Now consider the following array :

int arr[6]

The above can also be declared as :

int* arr;
arr = new int[6];

i mean to say that arrays are treated internally as pointers. Hence, both the following lines hold true inorder to access value at i(th) postion in arr[6] :
a or *(a+i)

both the above expressions mean the same thing.

If you talk about 2-D array, they can also be handled by either using an array of pointers (as you have used) or a pointer to an array; or the simplest is to use the 2-D array itself.
:)

>I think I could reduce my pointer headaches if only I could understand pointers better.
If you're having trouble wrapping your head around pointers, chances are good that you're thinking too hard. Beginners tend to overcomplicate the concept of pointers into some massively difficult thing.

>Somewhere in memory I've declared a character array (not an array of individual
>characters, but an array of variable length strings made up of characters)
What you've declared is an array of pointers to char.

>I want to point to a given element of that array, i.e., to one of those strings, name[0]
>through name[4], and then extract that character string and place it in a character string
>variable.
There's no magic involved. name[0] will give you a pointer to the first character of "ANGIE", name[1] will give you a pointer to the first character of "BEVERLY", and so on. To copy those strings into an array of char, you have to loop over the characters and copy them individually[1]:

for ( i = 0; name[name_int][i] != '\0'; i++ )
  name_sel[i] = name[name_int][i];
name_sel[i] = '\0';

name[name_int] is the string, which can be indexed just like an array. This gives you the syntax of name[name_int].

[1] The strcpy function in <cstring> does this.

>i mean to say that arrays are treated internally as pointers.
No, arrays are treated internally as arrays. Please don't make the mistake of thinking that arrays and pointers are the same. They're not, and slippery slopes lay waiting for the people who disagree.

>Hence, both the following lines hold true inorder
>to access value at i(th) postion in arr[6]: a or *(a+i)
Your statement is correct (assuming a stands for arr or a pointer to the first element of arr). I don't agree with the "hence" part though, because a[i] and *(a+i) are both acting on a pointer. Except for three specific cases, the name of an array is converted to a pointer to the first element of the array. That means a[i] is functionally equivalent to this:

// a[i]
( (int*)&a )[i]

And as you're vaguely aware, the array notation is syntactic sugar for a pointer offset calculation. The above is converted internally to this:

*( (int *)&a + i );

>i mean to say that arrays are treated internally as pointers.
No, arrays are treated internally as arrays. Please don't make the mistake of thinking that arrays and pointers are the same. They're not, and slippery slopes lay waiting for the people who disagree.

>Hence, both the following lines hold true inorder
>to access value at i(th) postion in arr[6]: a or *(a+i)
Your statement is correct (assuming a stands for arr or a pointer to the first element of arr). I don't agree with the "hence" part though, because a[i] and *(a+i) are both acting on a pointer. Except for three specific cases, the name of an array is converted to a pointer to the first element of the array. That means a[i] is functionally equivalent to this:

// a[i]
( (int*)&a )[i]

And as you're vaguely aware, the array notation is syntactic sugar for a pointer offset calculation. The above is converted internally to this:

*( (int *)&a + i );

Ok. thanks for pointing out my mistake; and as far as "a" is concerned it was my mistake.
I actually meant :

arr[i]
*(arr+i)

:)

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.