I am trying to parse a string passed in as an input parameter. Basically if the input string is under 50 characters I want to add additional zeros in the new string. If the input string is over 50 characters, just take the first 50. I am getting segmentation faults and could use a big of guidance.
Extra information: I have a struct in the beginning of the program defined as follows:
typedef struct Integer50
{
int *digits;
} Integer50;
ParseString Function:
Integer50 *parseString(char *str){
int x = 0, y = 0, temp = 0;
int length_string;
Integer50 *large_num;
large_num = malloc(sizeof(large_num));
int i;
if (str == NULL)
{
return NULL;
}
if(strlen(str) < MAX50){
fprintf(stderr, "Error, input string is less than fifty (50).\n");
for(i = 0; i < MAX50; i++){
large_num->digits[i] = str[i];
}
}
else{
for(i = 0; i < MAX50; i++){
large_num->digits[i] = str[i];
}
}
return large_num;
}
large_num->digits
large_num = malloc(sizeof(large_num));
allocate memory for the struct not the pointer in the struct.