1

Possible Duplicate:
What is the difference between char s[] and char *s in C?
Difference between char *str = “…” and char str[N] = “…”?

I have a structure defined as:

typedef struct
{
  bool                  configured;
  bool                  active;
  uint32                lastComms;
  uint32                rxChRvdTime;
  char                  *name;
}vehicle;

and I initialize it as follows:

static vehicle *myVehicle; 

When I want to initialize the name, I use:

myVehicle->name = "helloworld";

And this works fine. But when I wan't to set it to something other than a string literal, I seem to run into problems.

char *tmpName = "foobar";
strcpy(myVehicle->name, tmpName);

So why doesn't strcpy work? Do I somehow need to preallocate the string size in the structure before hand? Should I not be using a pointer for the 'name' field, since there can only be one vehicle?

Community
  • 1
  • 1
Jonathan
  • 1,498
  • 2
  • 20
  • 41

3 Answers3

2

You need to allocate memory to copy into:

 myVehicle->name = malloc(strlen(tmpName) + 1);
 if (myVehicle->name)
 {
     strcpy(myVehicle->name, tmpName);
 }

as myVehicle->name is an uninitialised char*. If you malloc() you need to free(). The assignment to the string literal works because you making name point to the address of the string literal, which exists for the lifetime of the program.

But before this, you need to allocate memory for myVehicle itself:

myVehicle = malloc(sizeof(*myVehicle));

or just make myVehicle an object rather than a pointer:

static vehicle myVehicle;
hmjd
  • 120,187
  • 20
  • 207
  • 252
0

You want

  myVehicle->name = strdup("foobar");

but don't forget to free it later.

Actually, you usually don't strdup constant literal strings (but you need to do that if that string could be overwritten later, since constant literal strings usually stay in read-only segments, and writing into them is undefined behavior.). You might build a string in some buffer, and strdup that buffer. A more realistic example might be:

 char buf[32];
 static int counter;
 counter++;
 snprintf(buf, sizeof(buf), "#%d", counter);
 myVehicle->name = strdup(buf);
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

You only declared a pointer, not actual memory it points to. try:

char *tmpName = "foobar";
myVehicle->name = malloc(strlen(tmpName) + 1);
if (myVehicle->name)
    strcpy(myVehicle->name, tmpName);
else
{
    // error
}

Don't forget to free myVehicle->name once you are done with it.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • wow. 32 seconds after i posted? Thanks! So when the function that this is contained in finishes, the code wont automatically clear the declared tmpName space? How does it know that I will still be using it later? – Jonathan Oct 20 '12 at 15:59
  • 1
    It will not be cleared, as you called malloc, and malloc'd memory is persistant until you explicitly free it (`free(myVehicle->name)`). If you lose the pointer before freeing the buffer, no one will clean it for you and you will get a memory leak. – MByD Oct 20 '12 at 16:00