1

我一直在尝试完成这段代码,但我一直在创建一个临时缓冲区。我以前从未学过这个,但不知何故我需要在我的程序中使用它。

这个网站我认为最好的选择是

char * func1() {
     char *buffer = (char *)malloc(1000);
     buffer[0] = '\0'; // Initialize buffer
     // Do processing to fill buffer
     return buffer;
}

以下是我的代码

 #include <stdio.h>
#include <stdlib.h>
#define LUNCHES 5
#define ARRAY 2

int main(void)
{
    int x;
    struct Food
    {
        char *name;                                                            /* “name” attribute of food */
        int weight, calories;                                                  /* “weight” and “calories” attributes of food */
    }lunch[LUNCHES] = { [0] = {"apple", 4, 100}, [1] = {"salad", 2, 80} };

    for(x = ARRAY; x < LUNCHES; ++x)
    {
        char *buff = malloc(sizeof(lunch[x].name));

        printf("Please input \"food\", weight, calories: ");
        scanf("%s", buff);
        scanf("%d %d", &lunch[x].weight, &lunch[x].calories);
        printf("The %s weighs %doz. and contains %d calories.\n", lunch[x].name, lunch[x].weight, lunch[x].calories);


    }

    return 0;
}

好的改变了。但现在的输出是

NULL 称重并包含 . 为什么为空?

已更正

#include <stdio.h>
#include <stdlib.h>
#define LUNCHES 5
#define ARRAY 2

int main(void)
{
    int x;
    struct Food
    {
        char *name;                                                            /* “name” attribute of food */
        int weight, calories;                                                  /* “weight” and “calories” attributes of food */
    }lunch[LUNCHES] = { [0] = {"apple", 4, 100}, [1] = {"salad", 2, 80} };

    for(x = ARRAY; x < LUNCHES; x++)
    {
        lunch[x].name = malloc(25 * sizeof(char));

        printf("Please input \"food\", weight, calories: ");
        scanf("%s", lunch[x].name);
        scanf("%d %d", &lunch[x].weight, &lunch[x].calories);
        printf("The %s weighs %doz. and contains %d calories.\n\n", lunch[x].name, lunch[x].weight, lunch[x].calories);

        free(lunch[x].name);

    }

    return 0;
}
4

1 回答 1

0

首先,它是for(x = ARRAY; x < LUNCHES; ++x)- 注意<不是<=,否则你会溢出数组(索引是从零开始的,它从0to运行LUNCHES-1)。

至于分配:

  • you need to create buffer for every entry in the lunch[] array, hence in the for loop you need something like lunch[x].name = malloc(SIZE), where SIZE is a reasonable value - for the purpose of meal name ~80 characters would seem more than enough;
  • next you must to check, that the pointer assigned to lunch[x].name is not NULL, which would signal out of memory condition - otherwise you might happen to cause a segmentation fault by dereferencing it;
  • then you can use the pointer (to the newly allocated buffer) as the argument to scanf(), but remember to specify the maximum width (i.e. SIZE-1), so that you don't overrun into unallocated memory.

Remember to use free() on the pointers to allocated memory when you don't need the data any more or at the end of the program - while in your simple example it's technically not necessary, it could easily start a very bad habit.

于 2012-11-12T22:07:07.123 回答