M_kul M_kul - 1 month ago 5
C Question

how to set string b to equal a when a has spaces in it c

I have a string called that can continue a value with spaces for it as an example

char *a="this is a test"


I want to declare another string

char *b=NULL;


so I can then assign the value of a to b such as

b=a;


however currently b will only hold any characters up to the first space (in this example b= "this").
How do I get it to hold the entire string

Answer

You did not really "declare" a string. With

char *a="this is a test";

you assigned a literal string to a character pointer. With:

char *b=NULL;
b=a;

you assigned what a points to b, so you still have only one string (literal). Note that string literals generally reside in read-only memory and cannot be modified. What you could do is:

char a[]="this is a test";
char b[80];
strcpy(b,a);

Now you have two strings and can manipulate each independently.