I am writing a simple struct and some functions to populate the struct. The struct is not typedef as it is a fairly small program. I am having trouble understanding why my array of structs does not increase in size when I use the function makeArray that uses malloc (see below in the code)?
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;
}
I feel like I am close to having this work as it does work if I try to edit the struct at arr[0] but it does not work for arr[1], arr[2], arr[3]. I'm not sure what I am missing in the function?
Below is the exact code I have where only the first entry into the array works? arr [0] (uncommented printf at the bottom)
#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;
}
CodePudding user response:
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;
}
Instead you would need to pass the address of the array element as an 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 \0 as well
strcpy_s(element->insides, element->size 1, text); // or strncpy
return element;
}
Use assert
to visualize the contract of the function, what must be true for the function to work.
To initialize using the function
arr = makeArray(4);
makeStuff(arr 2, "some text");
Also probably a good idea in your makeArray
function to initialize insides
for (int i = 0; i < size; newArray[i ]->insides=NULL);