Array of pointers to structs
I'm trying to write a queue for this project. Suppose to use a struct, got that part. We had to write this before, but just using ints not structs. I'm having problems adding something to the queue. Here's the basic layout:
I have a struct which can be referenced by using AddrT, or AddrTP which returns a pointer to a struct.
I have a an AddrT *x in main for an array of structs, this will be the queue.
Once the program gets run the user has the option of selecting "a" and typing in the customer ID, first, last name. At this part I have a an AddrT called toAdd, and an AddrTP called add here is the code for it
Code:
scanf("%d%s%s", &id,&first,&last);
toAdd.custID = id;
strcpy(toAdd.firstName, first);
strcpy(toAdd.lastName, last);
add = &toAdd;
printf("\n");
if(addCust(add) == 0)
printf("Not added\n");
addCust takes in an AddrTP called cust, and returns 1 if successful 0 if not, code for this method is:
Edit: I found the error, it should have been == NULL, derrrrr
Code:
if((x = realloc(x, (count + 1) * sizeof(AddrTP))) == NULL){
return 0;
}
x[count - 1] = cust[0];
count++;
return 1;
The problem is, whenever I run the program and select a and type in the information in, it always returns back a 0 and tells me it wasn't added. Any suggestions?