
assigning the value of a char to a string (newbie)
Quote:
>Say I have
> char character
>and
> char *string
>how do I assign the value of character to string?
You don't. character has char type and string has pointer to char type
and you really don't want to assign one to the other.
Quote:
>I've tried string=&character; this gives me the right character, but it also
>includes a lot of nonsense after it.
The fact that you have called a variable string doesn't make it a string
(or in this case a pointer to a string). It is up to you to set things
up correctly. A string is a sequence of characters terminated by a null
character and is held in an array of characters. A pointer to a string is a
pointer set to point to the first character of a string. ``character'' here
is just single character so can't hold a string (or no more than a zero
zero length string). What you need is an array e.g.
char str[SIZE]; /* choose a suitable size */
To create a string in it consisting of the single character held in
``character'' you can use
str[0] = character;
str[1] = '\0'; /* The null character required at the end of
every string */
You could make string above a pointer to this string by writing
string = str;
--
-----------------------------------------
-----------------------------------------