Okay, I'm trying to learn how to dynamically allocate memory, and I figure the best way to do it is to try to make something with it and learn as I go.
Code:
//main.c
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
int main(void)
{
//...
Room forest;
//...
initializeVerbs(&forest, 4, "get", "drop", "take", "use");
//...
return 0;
}
//parser.h --------------
#ifndef PARSER_H
#define PARSER_H
typedef struct Room
{
short unsigned nVerbs;
short unsigned nNouns;
char *verbs;
char *nouns;
} Room;
void initializeVerbs(struct Room *room, unsigned int nargs, const char *arg, ...);
//...
#endif
//parser.c ---------------
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include "parser.h"
void initializeVerbs(Room *room, unsigned int nArgs, const char *arg, ...)
{
unsigned count;
unsigned length = 0;
va_list ap;
char *stuff;
printf("Step 1.\n");
room->nVerbs = nArgs;
printf("Step 2.\n");
va_start(ap, arg);
for(count = 1; count < nArgs; count++)
{
length += (strlen(arg) + 1);
printf("sizeof(verbs): %i\n", sizeof(room->verbs));
printf("length = %i\n", length);
realloc(room->verbs, length); /*problem*/
room->verbs[length] = arg; /*Causes problems.*/
arg = va_arg(ap, const char *);
}
printf("Length: %i\n", length);
}
Okay, what I'm trying to do is this. I want the user to be able to initialize as many verbs as he wants, and I want them all in one char pointer. nVerbs holds how many there are, so I can search through them later by going past the null terminator.
However...I have no idea what I'm doing. Please help.
Thanks