M_kul M_kul - 1 year ago 62
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


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 Source

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;

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];

Now you have two strings and can manipulate each independently.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download