| | |
Error C2440: Char Array Addressing Problem
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
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:
Your help is appreciated.
"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:
C++ Syntax (Toggle Plain Text)
#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 );
Last edited by Ancient Dragon; Oct 1st, 2008 at 10:39 pm. Reason: add code tags
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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2008
Posts: 89
Reputation:
Solved Threads: 2
C++ Syntax (Toggle Plain Text)
Selected Name: ", STRLEN, name_num );
this line dint make much sense to me.

anyways..talking about your problem..
C++ Syntax (Toggle Plain Text)
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...
Bhoot
I would also prefer that you use c++ based console in and out mechanisms.
You should consider using
C++ Syntax (Toggle Plain Text)
printf ()
C++ Syntax (Toggle Plain Text)
cout<<
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
C++ Syntax (Toggle Plain Text)
Selected Name: ", STRLEN, name_num );
this line dint make much sense to me.
anyways..talking about your problem..
C++ Syntax (Toggle Plain Text)
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 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.
•
•
Join Date: Mar 2008
Posts: 89
Reputation:
Solved Threads: 2
•
•
•
•
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.
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 :
C++ Syntax (Toggle Plain Text)
int arr[6]
C++ Syntax (Toggle Plain Text)
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[i] 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.
Bhoot
>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]:
name[name_int] is the string, which can be indexed just like an array. This gives you the syntax of name[name_int][i].
[1] The strcpy function in <cstring> does this.
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]:
C++ Syntax (Toggle Plain Text)
for ( i = 0; name[name_int][i] != '\0'; i++ ) name_sel[i] = name[name_int][i]; name_sel[i] = '\0';
[1] The strcpy function in <cstring> does this.
New members chased away this month: 5
>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[i] 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
And as you're vaguely aware, the array notation is syntactic sugar for a pointer offset calculation. The above is converted internally to this:
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[i] 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: C++ Syntax (Toggle Plain Text)
// a[i] ( (int*)&a )[i]
C++ Syntax (Toggle Plain Text)
*( (int *)&a + i );
Last edited by Narue; Oct 2nd, 2008 at 3:41 pm.
New members chased away this month: 5
•
•
Join Date: Mar 2008
Posts: 89
Reputation:
Solved Threads: 2
•
•
•
•
>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[i] 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, becausea[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 meansa[i]is functionally equivalent to this:
And as you're vaguely aware, the array notation is syntactic sugar for a pointer offset calculation. The above is converted internally to this:C++ Syntax (Toggle Plain Text)
// a[i] ( (int*)&a )[i]
C++ Syntax (Toggle Plain Text)
*( (int *)&a + i );
I actually meant :
C++ Syntax (Toggle Plain Text)
arr[i] *(arr+i)
Bhoot
![]() |
Other Threads in the C++ Forum
- Previous Thread: traversing through a link list backward
- Next Thread: pls. help in my program
Views: 1094 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






