Question: So i'm trying to create a simple String struct which will contain the text of the string and the size of it, at least for now. However I'm having issues allocating my struct. Right now i'm simply trying to get a size of 1 character to work, but its simply crashing at this point and i don't know what i'm doing wrong with the allocation, please help.

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

typedef struct{
    char* text;
    int size;
}String;

String* getString();

int main(int argc, char** argv){

    String* str1 = getString();
    printf("%s",str1->text);    
    free(str1);
    return 0;
 }

String* getString(){
    String* str = (String*)malloc(sizeof(String));
    scanf("%s",str->text);
    str->size++;
    return str;
}

```

Answer: 
You don't allocate any memory for `str->text`. You leave it uninitialized, so your program invokes undefined behavior.

You need to allocate memory for it using `str->text = malloc(MAX_SIZE);` where `MAX_SIZE` is the maximal size for the string. Alternatively, if your strings will be short, you may use a regular, fixed-szie array instead.

Furthermore, you probably do not want to use `scanf()` for scanning strings. One particular reason is that `%s` makes `scanf()` stop at the first whitespace character. Another reason is that it's not trivial to prevent `scanf()` from writing past the buffer if it's too small.. How about using `fgets()` instead?

```
fgets(str->text, MAX_SIZE, stdin);

```

is a better and safer approach.
```
int main(int argc, char** argv){
    String* str1 = getString();
    printf("%s",str1->text);
    free(str1->text);
    free(str1);
    return 0;
 }

String* getString(){
    String* str = (String*)malloc(sizeof(String));//for struct
    str->size = 16;//initialize
    str->text = (char*)malloc(sizeof(char)*str->size);//for input text
    int ch, len;
    while(EOF!=(ch=fgetc(stdin)) && ch != '\n'){
        str->text[len++]=ch;
        if(str->size==len){
            str->text = realloc(str->text, sizeof(char)*(len+=16));
        }
    }
    str->text[len++]='\0';
    str->text = realloc(str->text, sizeof(char)*(str->size=len));
    return str;
}

```