I have a function
void add_car(char *car) {
park.buffer[park.size] = car;
park.size ++;
}
car is a string like “XYZ”. Atm, It seems to assign the carpark buffer array element to an address. Thus if I fill the array with 10 cars, I get back 10 cars, but each of them is the latest one, since the address of car was overwritten. How do I fill my carpark.buffer with the actual value of a car.
Total mindblank atm
Assuming
park.bufferis an array ofchar*, you may want to useNote that, inevitably, this makes a copy (indispensable, if you don’t want further changes to
carto affectpark.buffer!) owned bypark(meaning that, when done, you’ll have tofreeit from there, because strdup implicitly does amalloc[and astrcpytoo of course;-)]).BTW, you’ll need to
#include <string.h>at the top of your file (if you’re not already doing so) in order to usestrdup.