I have an array of strings, and I would like to extand it when it no longer has NULL pointers (meaning the array is full). I have tried realloc with no success, I think i'm not thinking right pointer-wise.
Here is my code:
int storage; //global, outside of main
int i, key;
char **people;
char **phones;
printf("Please enter a storage cacpity:\n");
scanf("%d",&storage);
printf("\n");
people=malloc(storage*sizeof(char *));
phones=malloc(storage*sizeof(char *));
for (i=0; i<storage; i++) {
people[i] = NULL;
phones[i] = NULL;
}
void AddNewContact(char * people[], char * phones[]) {
char name[100];
char phone[12];
int i, listfull = 0;
printf("Enter a contact name:\n");
scanf("%s",&name);
printf("Enter a phone number:\n");
scanf("%s",&phone);
for (i=0; i<storage; i++) {
if (people[i]==NULL) {
people[i] = (char *)malloc(strlen(name));
phones[i] = (char *)malloc(strlen(phone));
strcpy(people[i],name);
strcpy(phones[i],phone);
break;
}
listfull = 1;
}
if (listfull == 1) {
storage++;
people = realloc(&people,(storage)*sizeof(char *));
phones = realloc(&phones,(storage)*sizeof(char *));
people[storage-1] = NULL;
phones[storage-1] = NULL;
strcpy(people[storage-1],name);
printf("\nData Base extanded to %d",storage);
}
printf("\n");
return;
}
void PrintAll(char * people[], char * phones[]) {
int i;
for (i=0; i<storage; i++) {
if (NULL != people[i]) {
printf("Name: %s, ",people[i]);
printf("Number: %s\n",phones[i]);
}
}
printf("\n");
return;
}
Any help would be highly appreciated, I'm stuck on it for a few hours and having no luck sovling this.