ML
2013-02-18 17:04:37 UTC
int main(){
int i, sizeString1;
char *s1;
printf("Input: \t");
sizeString1 = make_string(&s1);
printf("Output: \t, %s", s1);
free(s1);
return 0;
}
String *make_string(char *s1){
char *a, b;
int i, size=0;
String *new = (String *) malloc(sizeof(String));
//a = new->chars; <--- problem only created space for struct not the dynamic array
a = (char *) malloc(sizeof(char));
b = getchar();
while(b != '\n' && size < MAX){
*(a+size) = b;
size++;
b = getchar();
a = realloc(a, size+1);
}
*(a+size) = '\0'; //end of String marker
new->chars = a;
new->length = size;
*s1 = *a;
return new;
}
I defined a String as
typedef struct{
char *chars;
int length;
} String;