为什么 malloc 不接受全文?

why does malloc not taking in the full text?

为什么当我尝试在变量 is 中输入文本时,它没有存储在完整的句子中。 malloc 似乎没有为字符串分配足够的内存,为什么?

所以对于 'second' 变量,当我输入 'is not happy why' 但是 'second' 仅存储 'is no' 时应该有足够的 space 用于字符串,为什么?

当我尝试下面的代码时:

#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] = '[=10=]'; // 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] = '[=10=]'; // 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 */

输出是:

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

好的,所以当您调用 fgets 时,您传递了指针的大小。不是它指向的大小。

因此,例如,现在它正在运行:

#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)] = '[=10=]'; // 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)] = '[=10=]'; // 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;
}

您需要将实际大小而不是指针的大小传递给 fgets :)