Error C2440: Char Array Addressing Problem

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

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

Error C2440: Char Array Addressing Problem

 
0
  #1
Oct 1st, 2008
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:
  1. #define STRLEN 50
  2.  
  3. Char *name[] =
  4. {
  5. "ANGIE",
  6. "BEVERLY",
  7. "CARLOTTA",
  8. "DANIELLE",
  9. "EVELYN"
  10. };
  11.  
  12. Char name_num [STRLEN];
  13. Char name_sel [STRLEN];
  14. Int name_int;
  15. Int i;
  16.  
  17. Selected Name: ", STRLEN, name_num );
  18.  
  19. printf ( "Name_number = %s\n", name_num );
  20.  
  21. name_int = atoi( name_num );
  22.  
  23. printf ( "name_int: %i\n", name_int);
  24.  
  25. name_sel = *name [ name_int ];
  26.  
  27. for ( i=0; i<5; i++)
  28. {
  29. printf ( "The Friend's Name is: %s\n", name[i]);
  30. }
  31.  
  32. printf ( "Name_sel = %s\n", name_sel );
  33.  
Your help is appreciated.
Last edited by Ancient Dragon; Oct 1st, 2008 at 10:39 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Error C2440: Char Array Addressing Problem

 
0
  #2
Oct 1st, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: Error C2440: Char Array Addressing Problem

 
0
  #3
Oct 2nd, 2008
  1. Selected Name: ", STRLEN, name_num );
  2.  

this line dint make much sense to me.

anyways..talking about your problem..

  1. 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Error C2440: Char Array Addressing Problem

 
0
  #4
Oct 2nd, 2008
I would also prefer that you use c++ based console in and out mechanisms.

  1. printf ()
You should consider using
  1. cout<<
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: Stringman is an unknown quantity at this point 
Solved Threads: 0
Stringman Stringman is offline Offline
Newbie Poster

Re: Error C2440: Char Array Addressing Problem

 
0
  #5
Oct 2nd, 2008
Originally Posted by Ancient Dragon View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: Stringman is an unknown quantity at this point 
Solved Threads: 0
Stringman Stringman is offline Offline
Newbie Poster

Re: Error C2440: Char Array Addressing Problem

 
0
  #6
Oct 2nd, 2008
Originally Posted by bhoot_jb View Post
  1. Selected Name: ", STRLEN, name_num );
  2.  

this line dint make much sense to me.

anyways..talking about your problem..

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: Error C2440: Char Array Addressing Problem

 
0
  #7
Oct 2nd, 2008
Originally Posted by Stringman View Post
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 :
  1. int arr[6]
The above can also be declared as :
  1. int* arr;
  2. 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Error C2440: Char Array Addressing Problem

 
1
  #8
Oct 2nd, 2008
>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]:
  1. for ( i = 0; name[name_int][i] != '\0'; i++ )
  2. name_sel[i] = name[name_int][i];
  3. 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][i].

[1] The strcpy function in <cstring> does this.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Error C2440: Char Array Addressing Problem

 
1
  #9
Oct 2nd, 2008
>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 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:
  1. // a[i]
  2. ( (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:
  1. *( (int *)&a + i );
Last edited by Narue; Oct 2nd, 2008 at 3:41 pm.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: Error C2440: Char Array Addressing Problem

 
0
  #10
Oct 3rd, 2008
Originally Posted by Narue View Post
>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 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:
  1. // a[i]
  2. ( (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:
  1. *( (int *)&a + i );
Ok. thanks for pointing out my mistake; and as far as "a[i]" is concerned it was my mistake.
I actually meant :
  1. arr[i]
  2. *(arr+i)
Bhoot
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum


Views: 1094 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC