Skip to main content
gamedev.net
🔒 Locked

String Question

Started by programmer2 0 replies 300+ views
Original Post
programmer2
programmer2
I know how to make a string by using an array, like mystring[10] But then when I try to assign a value to it, it says no l-value on the left side of the equals sign. I''ve tried strcpy and looked around, but I can''t figure out how to declare a string in one spot and assign a value to it in another. I also get an error using switch(), if I fill all the cases up with string value assignments, it says I''m initializing it more than once. It gave me about 90 errors at once for a big block of them. How do I fix this?
Null and Void
Null and Void
You can''t assign a pointer to an array. You can''t switch an array or a pointer. You can use strcpy like this:

#include <string.h>

char my_string[10];
strcpy(my_string,"abcdefghi");

/* Now, my_string contains:
my_string[0] ''a''
my_string[1] ''b''
my_string[2] ''c''
my_string[3] ''d''
my_string[4] ''e''
my_string[5] ''f''
my_string[6] ''g''
my_string[7] ''h''
my_string[8] ''i''
my_string[9] ''\0''
*/

To test if a string is equal to another, you can use strcmp:

#include <string.h>

if(strcmp(my_string,"abcdefghi") == 0) {
/* The strings are equal */
} else {
/* The strings are not equal */
}

You can use the STL''s string class to get around much of this, if you''re programming in C++ (as oppossed to C).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.