-
Character Arrays
I am new here and I have a problem. It may have been discused in one of the 16911 other threads but I have limited time to search for it currently. My problem is that when I type the following I get an error:
Code:
int main
{
char bob[9];
bob = "hello";
}
I have tried variations on this and the only thing that works is:
Code:
int main
{
char bob[9];
bob[0] = 'h';
bob[1] = 'e';
bob[2] = 'l';
bob[3] = 'l';
bob[4] = 'o';
}
It may be that it is a Dev problem if so please tell me, I am getting tired of all the errors its causing when I forget
-
Use / not \ in code tags
And its
char bob[9] = "hello";
or
strcpy( bob, "hello" );
-
you are trying to assign a string literal to a null terminated char array, but the assignment operator isn't assigned for that task. you could do this:
char bob[9];
cout << "enter a word of under nine characters" << endl;
cin >> bob;
cout << "you entered " << bob << endl;
our you could do this:
char bob[9];
strcpy(bob, "hello");
cout << bob;
strcpy and other functions to manipulate C style strings (aka null terminated char arrays) are available in cstring or string.h header files, whichever is available with your compiler.
-
strcpy should work, I tried
char bob[9] = "hello";
and it didn't work. Also, getting it from cin is not what I want because it is going to be a text game and the variable I want set I don't want the user to set.
-
How about posting a complete program which shows the problem (as you see it)
One line snippets taken way out of context just don't cut it.
There is nothing inherently wrong with
char bob[9] = "hello";
-
how about creating a pointer to a string
Code:
char *str= "smith";
OR
char *str;
cout << "enter new name";
cin >> str;
:)
-
>how about creating a pointer to a string
How about not?
>char *str= "smith";
This is actually a deprecated feature in C++. str should be a const char * to be fully correct, not a char *.
>char *str;
>cin >> str;
str doesn't point to any memory you own, so trying to store data at that address is a Very Bad Thing.
-
What's wrong with the following?
Code:
char *str[] = { "hiya" };
-
>What's wrong with the following?
>char *str[] = { "hiya" };
The same as above. You just hid it behind the declaration of an array of size 1.
-
I've read quite a few books that follow the ANSI standard and they teach it this way. I understand why you should make it const though.
-
Salem, its not the whole program because I get the same error in all of my programs. I think its just my compiler.
Also strcpy worked fine.
-
Sorry if this seems like a silly question....
Why the const? What is this going to do and why is it important?
-
Until Smiley27 actually gets around to posting a complete program which can be compiled - with whatever problem is present, further discussion is moot
-
how can you get
Code:
char bob[5] = "hello";
to compile tho? i always have to use strcpy.
-
Because in C++, you cannot silently drop the \0 from a string initaliser (but you can in C)
Normally, you don't specify a size for the array, you let the compiler do it
char bob[] = "hello";
and the whole problem goes away
Besides,
char bob[5];
strcpy( bob, "hello" );
is a buffer overflow, so you're really much worse off despite your apparent progress.