0

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
Oh Oh
  • 35
  • 3
  • 4
    The size of a pointer is the size of the pointer itself, not to whatever it might point to. Also, you do know that strings are null-terminated, but remember that the space you allocate need to include this terminator. So a five-character string need space for *six* characters. – Some programmer dude May 13 '22 at 10:01
  • 3
    5 bytes is not enough to store `"jason\n\0"` ayway. You are removing the newline *after* it was read. – Weather Vane May 13 '22 at 10:01
  • I've added two duplicate links, one tells how to use strings, the other how to use fgets. – Lundin May 13 '22 at 10:27
  • hi, so the issue was that fgets was not in the right size, is there anyway to use fgets with realloc or the pointer so that it will allocate the right size? without me doing some thing like this: str = malloc(200 * sizeof(char)); AND fgets(str, 200, stdin); – Oh Oh May 13 '22 at 10:27
  • @OhOh Just allocate 128 bytes or so to begin with. – Lundin May 13 '22 at 10:28
  • You can call `fgets` in a loop to append to the string until it has read a full line (the buffer contains a newline), and reallocate as needed. – Some programmer dude May 13 '22 at 10:31

1 Answers1

0

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 :)

al366io
  • 87
  • 8
  • is there a way to match fgets size to the pointer allocated size without explicitly writing the same size in the fgets ? – Oh Oh May 13 '22 at 10:32
  • @OhOh umh not really because you have to know how much memory to allocate. And you cannot change array size during runtime. One thing you can do is using some `int tempLenght = 200` then `fgets(second, tempLenght, stdin) ` and reallocating `second` with realloc i guess.... – al366io May 13 '22 at 12:38