I'm trying to make a simple database program, but when I get to this line
int idSearch(product* itens, int id) {
int i = 0;
for(i; i < size; i++) {
if(itens[i].id == id) //<=== This line
return i;
}
return ITEM_NOT_FOUND;
}
the program stops responding. size is set as a global variable in the begining of the program
FILE* flog;
FILE* db;
FILE* sizef;
int size = 100;
this function is called by
void newProduct(product* itens, char name[64], int id, float price) {
int pos = idSearch(itens, 0);
if(idSearch(itens, id) != ITEM_NOT_FOUND) {
printf("Erro, o produto ja existe");
return;
}...
items is defined as
itens = (product*) calloc(sizeof(product), size);
and product is a struct defined as such
typedef struct{
char name[64];
int id;
float price;
int amount;
} product;
Firstly I thought the problem was that I was not using the -> operator, but when I tried the compiler says its not right.
I'm using Code::Blocks with GCC compiler on a Windows 7 x64
**EDIT: the whole code can be found here: http://hastebin.com/atulajubar.tex
Hope to hear answers soon, Thanks in advance
sizedefined in your first block of code? And what is it defined to?sizeis defined as a global variable that was set to 100 in the main function. It was initialized as 0 thoughitens? That is, from where you passed it tonewProduct?itensallocated? What size?newProductandidSearchshould have the proper size passed as an additional parameter. As written it is simply not possible to answer your question as you've given no reproducible steps, so guesses and speculation are the best you can hope for (and doesn't make for a good question, btw). My advise. lose the globalsizemake it local to the creator of your array, and pass it along with the array base address as a param where needed. (andcallocis fine if used right).