为什么我的结构数组没有正确初始化?

Why is my array of structs not initializing properly?

我正在编写一个简单的结构和一些函数来填充该结构。该结构不是 typedef,因为它是一个相当小的程序。 我无法理解为什么当我使用使用 malloc 的函数 makeArray 时我的结构数组的大小没有增加(见下面的代码)?

struct stuff{
    char * insides;   //a C-string
    int size;         //stores the string length of insides
};

/*This function creates a dynamic array of struct stuff of size size. Returns a pointer to the array*/

struct stuff * makeArray(int size){

    struct stuff *newarray = malloc(size * sizeof(struct stuff));
    //testing malloc
    if (newarray==NULL){
        fprintf(stderr,"ERROR: malloc failed\n");
        exit(EXIT_FAILURE);
    }
    return array;
}
/*Given a C-string input, this function makes a new struct stuff with that information stored and returns a pointer to it */

struct stuff * makeStuff(char *text){

    struct stuff * stuff;

    strcpy(stuff->insides, text);
    stuff->size =  strlen(text);

    return stuff;
}
/*Given a valid struct stuff pointer, this function prints the stored string to standard output*/

void printStuff(struct stuff * ptr);

        printf("The insides of stuff is %s", ptr->insides);


// This is my main but it doesn't give an output
int main()
{
    struct stuff* arr;     //make arr of struct stuffs
    arr = makeArray(4);    //increase dynamic array to 4

    //I would then need to use the function makeStuff 4 times in order to add a string and
    // the size of the string to each struct in the array 


//I use a loop with printStuff to print the contents of the array. 

//Free malloc function.

return 0;
}

我觉得我快要完成这项工作了,因为如果我尝试编辑 arr[0] 处的结构它确实有效,但它不适用于 arr[1]、arr[2]、arr[3 ].我不确定函数中缺少什么?

下面是我的确切代码,其中只有数组的第一个条目有效? arr [0](底部未注释的 printf)

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

struct stuff{
    char * insides;   //a C-string
    int size;         //stores the string length of insides
};


/*This function creates a dynamic array of struct stuff of size size. Returns a pointer to the array*/


struct stuff * makeArray(int size){

    struct stuff *newarray = malloc(size * sizeof(struct stuff));

    if (newarray==NULL){
        fprintf(stderr,"ERROR: malloc failed\n");
        exit(EXIT_FAILURE);
    }
    return newarray;

}



/*Given a C-string input, this function makes a new struct stuff with that information stored and returns a pointer to it */

struct stuff * makeStuff(char *text){

    struct stuff * stuff;

    strcpy(stuff->insides, text);
    stuff->size =  strlen(text);

    return stuff;
}

/*Given a valid struct stuff pointer, this function prints the stored string to standard output*/

//void printStuff(struct stuff * ptr);

        //printf("The insides of stuff is %s", ptr->insides);

/*This function takes a dynamic array of struct stuff and frees it along with all its dynamically allocated components*/

void freeStuff(int size, struct stuff * myArray);



int main()
{
    struct stuff* arr;     //make arr of struct stuffs
    arr = makeArray(4);    //

    strcpy(arr[0].insides, "this is a test");
    arr[0].size = 9898;


    //strcpy(arr[1].insides, "this is a test1");
    //arr[1].size = 4512;
//    strcpy(arr[2].insides, "this is a test2");
//    arr[2].size = 0000;
//    strcpy(arr[3].insides, "this is a test3");
//    arr[3].size = 2131;

    printf("the size of the first location is %d and %s", arr[0].size, arr[0].insides);
    //printf("the size of the first location is %d and %s", arr[1].size, arr[1].insides);
//    printf("the size of the first location is %d and %s", arr[2].size, arr[2].insides);
//    printf("the size of the first location is %d and %s", arr[3].size, arr[3].insides);
return 0;
}
struct stuff * makeStuff(char *text)
{
  // stuff does not point anywhere, it is uninitialized
  struct stuff * stuff;

  // in addition, you need to allocate memory to hold 'text'
  strcpy(stuff->insides, text);
  stuff->size =  strlen(text);

  return stuff;
}

相反,您需要将数组元素的地址作为输入参数传递。

struct stuff * makeStuff(struct stuff* element, char *text)
{
  assert(element != NULL);
  assert(text != NULL);
  element->size =  strlen(text);
  element->insides = malloc(element->size+1); // room for [=11=] as well
  strcpy_s(element->insides, element->size + 1, text); // or strncpy
  return element;
}

使用 assert 可视化函数的契约,函数工作必须满足的条件。

使用函数初始化

arr = makeArray(4);
makeStuff(arr + 2, "some text");

在你的 makeArray 函数中初始化 insides

也可能是个好主意
for (int i = 0; i < size; newArray[i++]->insides=NULL);