> You have not provided sufficient information for me to help you
> effectively. On general principles, lose the cast, replace sizeof(book)
> with sizeof *bp, use a temp to catch the result of realloc in case it
> fails and returns NULL, and make sure you have #included <stdlib.h>.
Note
> that bp is a copy of the calling function's pointer; the original will
not
> be updated automatically, so if you want to keep the new value you must
> provide a mechanism for the caller to assign this new value to the
> original pointer.
here's the full function.
i call it like this:
bp = AddBook(bp, &BOOK_SIZE);
/*#############################
# Add Book Function #
#############################*/
book* AddBook(book *bp, unsigned *size)
{
book temp;
unsigned n = *size; //n = size of book array
bool check = FALSE;
char str_cn[80];
int i;
printf("%d", (int)(sizeof(bp)/sizeof(bp[0])));
n++;
bp = (book*)realloc(bp, sizeof(book));
if (bp == NULL) { perror("ERROR [realloc - AddBook()]");
exit(ERR_REALOC); }
printf("\n[Add Book to Catalog]\n");
//get catalog number
while(check != TRUE)
{
printf("Book Catalog Number: ");
_flushall();
gets(str_cn);
//check if string is longer then 9 digits. if longer then error.
if(strlen(str_cn) > 9) { check = FALSE; printf("[E] catalog number
too long, 9 digit max.\n"); continue; }
//check each letter in string if a digit, if all are then check =
TRUE, else check = FALSE
i=0;
while(str_cn[i])
{
if(isdigit(str_cn[i])) { check = TRUE; i++; continue; }
else { check = FALSE; printf("[E] catalog number not valid, try
again.\n"); break; }
}
if(check == TRUE) { temp.cn = atol(str_cn); } //if all OK, put value
in str_cn
}
check = FALSE;
... here i get data into temp, and check validation ...
*size = n;
bp[n-1].cn = temp.cn;
bp[n-1].Units = temp.Units;
bp[n-1].Year = temp.Year;
bp[n-1].Price.CostPrice = temp.Price.CostPrice;
bp[n-1].Price.RetailPrice = temp.Price.RetailPrice;
strcpy(bp[n-1].Publisher, temp.Publisher);
strcpy(bp[n-1].BookName, temp.BookName);
strcpy(bp[n-1].Author.Author1, temp.Author.Author1);
strcpy(bp[n-1].Author.Author2, temp.Author.Author2);
strcpy(bp[n-1].Author.Author3, temp.Author.Author3);
return bp;
}


|
16 Posts in Topic:
|
Igal <igal.alkon@[EMAI |
2008-05-08 07:15:19 |
|
Richard Heathfield <rj |
2008-05-08 14:27:23 |
|
Igal <igal.alkon@[EMAI |
2008-05-08 07:33:01 |
|
"Jim Langston" |
2008-05-08 07:52:48 |
|
"Jim Langston" |
2008-05-08 07:36:48 |
|
Igal <igal.alkon@[EMAI |
2008-05-08 07:37:40 |
|
Igal <igal.alkon@[EMAI |
2008-05-08 07:43:38 |
|
"Jim Langston" |
2008-05-08 07:54:42 |
|
Kenneth Brody <kenbrod |
2008-05-08 10:56:41 |
|
"K. Jennings" & |
2008-05-08 18:57:08 |
|
Eric Sosman <Eric.Sosm |
2008-05-08 13:55:17 |
|
Kenneth Brody <kenbrod |
2008-05-08 13:46:46 |
|
CBFalconer <cbfalconer |
2008-05-08 18:17:43 |
|
Richard Heathfield <rj |
2008-05-09 05:32:33 |
|
Antoninus Twink <nospa |
2008-05-09 11:52:02 |
|
Eligiusz Narutowicz<el |
2008-05-09 11:54:42 |
|