# char* in structs



## bkouhi (Jun 5, 2013)

Hi.

I've been Googling for about three hours for this problem but no success. I have a struct:


```
typedef struct
{
        char* name;
} std;

std *st1;
```

How can I initialize the char* in that struct? I tried this but it does not work:


```
st1->name = (char*) malloc(sizeof(char) * BUFSIZ);
```

I also tried:


```
strcpy(st1->name, "Hello");
```

But no success again.


----------



## tmw (Jun 5, 2013)

Hello,


```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUF 6 
typedef struct structname {
  char *name;
} std;

int
main(void) {
  std *st1;
  st1 = malloc(sizeof(std));
  st1->name = malloc(sizeof(char) * BUF);
  strcpy(st1->name, "hello");

  printf("%s\n", st1->name);

  return 0;
}
```

Do you know now what you did wrong?


----------



## bkouhi (Jun 5, 2013)

Thanks for your help :e I should initialize the structure itself at the first place then initialize the char*. But is this the best way to store a string within an structure? I mean, how do real programmers put the strings in the structures?


----------



## kpa (Jun 5, 2013)

C doesn't give you that many choi*c*es. In the above example the string is of unknown length and the memory is allocated for it before it is initialized. In many cases the upper limit to the length is known in advance, for example the length of a path is limited by PATH_MAX macro. In such cases the memory could be allocated by the compiler:


```
typedef struct structname {
  char name[PATH_MAX + 1];
} std;
```

Always remember to check for overflows and use copy functions of the n variety, for example strncpy(3) and not just the plain strcpy(3). Also be aware of the extra zero byte and the end of C strings and allocate one more byte for it just to sure it can fit into the string.


```
int
main(void) {
  std *st1;
  st1 = malloc(sizeof(std));
  st1->name = calloc(BUF + 1, sizeof(char));
  strncpy(st1->name, somestring,  BUF);

  printf("%s\n", st1->name);

  return 0;
}
```

I changed the malloc(3) to calloc(3) to be sure the allocated memory (including the extra byte at the end!) is initialized to zero. There's a tricky detail about strncpy(3) when the length of the string that is to be copied is of equal length to the receiving buffer. The destination buffer won't be zero-terminated in that case and you have to do it yourself or do it like I did it above and somehow guarantee that the buffer was filled with zeros to begin with.

This and many other things have turned me away from C because it's really hard to get all the nitty gritty details right. In my opinion string handling can be just as well be done in a scripting language without any significant performance loss and in much much more secure manner. If you must write in C look into the option of using C++ and its strings and other data structures to avoid reinventing the wheel but then write the parts that have to be written in low level manner in C. 

(The above example leaves out all the error handling when memory allocations fail and that's a whole another matter that just adds to the complexity of C programs).

Oh and one thing. Real programmers use frameworks nowadays. Constantly reinventing the wheel is seen as waste of time and effort now.


----------

