Search code examples
cmallocfgetsrealloc

why does malloc not taking in the full text?


Why is it when I try to take in a text in the variable is does not store in the full sentence. The malloc seems to not allocate enough memory for the string why ?

so for the 'second' variable, when i put in 'is not happy why' BUT 'second' only stores 'is no' when is should have more than enough space for the string, why ?

when I try the code below :

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

/* Function Declerations */
char *str;
char *second;

/* Global Variables */

int main(){
    /* Initializing Global Variables */

    /* EXPERIMENTING WITH FGETS */
    /* Initial memory allocation */
    str = malloc(5 * sizeof(char));
    second = malloc(200 * sizeof(char));
    /* Get user input and print results */
    printf("Enter a string: "); // ask user to put in a string
    fgets(str, sizeof(str), stdin);
    str[strlen(str) - 1] = '\0'; // Removes new line character of fgets
    printf("Enter a another string: "); // ask ujason is a godser to put in a string
    fgets(second, sizeof(second), stdin);
    second[strlen(second) - 1] = '\0'; // Removes new line character of fgets
    printf("String = %s, Address of String is = %p\n", str, str);
    printf("String = %s, Address of String is = %p\n\n\n", second, second);

    
    /* Reallocating memory */
    str = (char *)realloc(str, (100 * sizeof(char)));
    printf("Combine text\n");
    strcat(str, second);
    printf("String = %s, Address of String is = %p\n", str, str);
    printf("String = %s, Address of String is = %p\n\n\n", second, second);

    free(str);
    free(second);
    return 0;
}

/* Function Details */

the output is:

Enter a string: jason
Enter a another string:  is not happy why
String = jason, Address of String is = 00000000001C2460
String =  is no, Address of String is = 00000000001C5FD0


Combine text
String = jason is no, Address of String is = 00000000001C70B0
String =  is no, Address of String is = 00000000001C5FD0

Solution

  • Okay so when you call the fgets, you pass the size of the pointer. Not the size of wathever it points to.

    So, for example, now it's working:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    /* Function Declerations */
    char *str;
    char *second;
    
    /* Global Variables */
    
    int main(){
        /* Initializing Global Variables */
    
        /* EXPERIMENTING WITH FGETS */
        /* Initial memory allocation */
        str = (char*) malloc(7 * sizeof(char));
        second =  (char*) malloc(200 * sizeof(char));
        /* Get user input and print results */
        printf("Enter a string: "); // ask user to put in a string
        fgets(str, 7, stdin);
        str[strlen(str)] = '\0'; // Removes new line character of fgets
        printf("Enter a another string: "); // ask ujason is a godser to put in a string
        fgets(second, 200, stdin);
        second[strlen(second)] = '\0'; // Removes new line character of fgets
        printf("\nString = %s, Address of String is = %p\n", str, str);
        printf("String = %s, Address of String is = %p\n\n\n", second, second);
    
        
        /* Reallocating memory */
        str = (char *)realloc(str, (100 * sizeof(char)));
        printf("Combine text\n");
        strcat(str, second);
        printf("String = %s, Address of String is = %p\n", str, str);
        printf("String = %s, Address of String is = %p\n\n\n", second, second);
    
        free(str);
        free(second);
        return 0;
    }
    

    You need to pass to fgets the actual size not the size of the pointer :)