assigning the value of a char to a string (newbie) 
Author Message
 assigning the value of a char to a string (newbie)

Say I have
    char character
and
    char *string

how do I assign the value of character to string?
I've tried string=&character; this gives me the right character, but it also
includes a lot of nonsense after it.

Thanks



Sat, 23 Feb 2002 03:00:00 GMT  
 assigning the value of a char to a string (newbie)

   Say I have
       char character
   and
       char *string

   how do I assign the value of character to string?

string[0] = character;
string[1] = '\0';

Make sure you've allocated space for the string first.
--
Dann Corbit on comp.lang.c:
"Once you have leached enough [from c.l.c], you may find that others are
 then able to attach their razor-sharp hooks into your meaty extremities
 and you can provide them with a gruesome meal yourself."



Sat, 23 Feb 2002 03:00:00 GMT  
 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?
> I've tried string=&character; this gives me the right character, but it also
> includes a lot of nonsense after it.

As I understand your question, there are several ways to do it.
One approach:

#include <stdio.h>

char character = 'a';
char string[2];

sprintf(string,"%c",character);

--
Al Bowers
Tampa, FL  USA

http://www.gate.net/~abowers/



Sat, 23 Feb 2002 03:00:00 GMT  
 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;

--
-----------------------------------------


-----------------------------------------



Sat, 23 Feb 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. assigning int value to char

2. Problem passing char * to a function where it is assigned its value

3. Assigning char * value to a CString object

4. Newbie - How to assign a pointer from a char

5. how do I assign a value to string

6. How to assign a Char to a C++ String

7. Question:: assigning a string value to a CString object

8. Assigning a char* to STL string not working

9. HowTo?: assigning a char or string with a \u Unicode escape sequence or 0x Hex code

10. problem assigning char* to char*

11. Adding char string values

12. char string of Hex values?

 

 
Powered by phpBB® Forum Software