|
-
May 23rd, 2012, 08:38 AM
#1
character array please Help!!!
void main()
{
char name[20];
name = "omshanti";
cout << name;
}
In the above program , if we won't initialize name as the type of its definition , why there's compile time error :
cannot convert from 'char [9]' to 'char [20]'.???????
while using other data type such as int , float , double , we can initialize them after defining them .But same is not the case with character string. WHY???????
TANUSHREE-AGRAWAL...
-
May 23rd, 2012, 08:51 AM
#2
Re: character array please Help!!!
But same is not the case with character string.
Because a string is an array of characters. Use memcpy to put characters in a character array.
Edit : That needs to be strcpy.
Last edited by Skizmo; May 23rd, 2012 at 10:00 AM.
-
May 23rd, 2012, 08:52 AM
#3
Re: character array please Help!!!
You can't assign a string literal to a char array like that. Use strcpy.
-
May 23rd, 2012, 08:53 AM
#4
Re: character array please Help!!!
 Originally Posted by Skizmo
Because a string is an array of characters. Use memcpy to put characters in a character array.
Why memcpy instead of strcpy?
-
May 23rd, 2012, 09:59 AM
#5
Re: character array please Help!!!
 Originally Posted by GCDEF
Why memcpy instead of strcpy?
Doh... sorry. Not having my day today. Of course it has to be strcpy.
-
May 23rd, 2012, 11:23 AM
#6
Re: character array please Help!!!
Code:
strcpy(name,"omshanti");
-
May 23rd, 2012, 12:13 PM
#7
Re: character array please Help!!!
 Originally Posted by Tanushreeagr
Code:
void main()
{
...
}
You are using incorrect signature for main(): it must be int instead of void and it must return an integer value.
Victor Nijegorodov
-
May 23rd, 2012, 02:08 PM
#8
Re: character array please Help!!!
Why not std::string?
Code:
#include <string>
// ...
std::string name = "omshanti";
or
Code:
#include <string>
// ...
std::string name;
name = "omshanti";
-
May 23rd, 2012, 05:00 PM
#9
Re: character array please Help!!!
 Originally Posted by Tanushreeagr
while using other data type such as int , float , double , we can initialize them after defining them .But same is not the case with character string. WHY???????
You can't do that with other built-in types either.
Code:
int main()
{
int a[3];
a = { 1, 2, 3 }; // error
}
You cannot assign to an array.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
May 23rd, 2012, 05:34 PM
#10
Re: character array please Help!!!
Also, in the case of char name[20] both name and "omshanti" are interpreted by the compiler as pointers. name is essentially the same thing as &name[0].
So you have a pointer to a locally allocated array of chars and you want to assign it the address of a string literal. Doesn't really make conceptual sense.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|