You're using realloc
incorrectly. You must accept the return value of realloc
in a variable different from that used as the first parameter since realloc
can potentially return NULL
. If it isn't NULL
, you can assign it to the original variable; but if it's NULL
, you still have a pointer to the previous memory (avoiding a memory leak).
There are other things wrong with your code.
sizeof
does not return the length of a string, but the size of the object the variable references, in this case it's a pointer, so it'll return 4 or 8. Use strlen
instead.
You're using firstname
and lastname
as if they're local variables. The values you assign to them disappear when the function returns. You'll need to pass in a triple pointer and dereference them on use.
You are trying to realloc space for a string, but you haven't attempted to reallocate any space for another pointer in the array to actually point to the new string memory.
You can't copy a string with a simple assignment statement; use strcpy instead.
And you're not passing the variables correctly, either. If they are to (potentially) receive new values, you need to pass the address, not the value.
So, it looks like you're trying to do this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void add(
char *** firstname, char *** lastname, float ** score,
char * newfirst, char * newlast, float newscore,
int index)
{
char ** t;
float * f;
t = realloc(*firstname, (index + 1) * sizeof(*firstname));
if (!t) ; // handle error
*firstname = t;
(*firstname)[index] = malloc(strlen(newfirst) + 1);
strcpy((*firstname)[index], newfirst);
t = realloc(*lastname, (index + 1) * sizeof(*lastname));
if (!t) ; // handle error
*lastname = t;
(*lastname)[index] = malloc(strlen(newlast) + 1);
strcpy((*lastname)[index], newlast);
f = realloc(*score, (index + 1) * sizeof(*score));
if (!f) ; // handle error
*score = f;
(*score)[index] = newscore;
}
int main() {
char **firstname = NULL, **lastname = NULL;
float *score = NULL;
int num = 0;
add(&firstname, &lastname, &score, "one", "two", 1.1f, num++);
add(&firstname, &lastname, &score, "three", "four", 2.2f, num++);
add(&firstname, &lastname, &score, "five", "six", 3.3f, num++);
for (int i = 0; i < num; ++i)
printf("%s %s %f\n", firstname[i], lastname[i], score[i]);
return 0;
}
Note that only the pointer arrays are realloced. The string memory is simply malloced.
It's pretty inefficient to realloc for a single element each time. Normally you'd realloc a chunk of elements at a time. Such an array is best represented as a struct so that its currently allocated space and currently occupied space can be saved together with the data.