我正在编写一个简单的结构和一些函数来填充该结构。该结构不是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 failedn");
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 failedn");
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;
}
相反,您需要将数组元素的地址作为inparameter传递。
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 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);