some_id
some_id

Reputation: 29886

Pointer troubles

I have an issue with the following code.

int main (int argc, const char * argv[]) {

#define MAXCHARACTERS 10
#define MAXNUMBERS 2

char buffer[MAXCHARACTERS];

numberOfStructs = 0;

allocSize = 10;
array = malloc(allocSize * sizeof(StructItem));
dataLink *link;

do
{
    Album *tempStruct;

    fgets(&(*tempStruct->firstField),MAXCHARACTERS, stdin);
    fgets(&(*tempStruct->secondField), MAXCHARACTERS, stdin);
    fgets(buffer, MAXNUMBERS, stdin);
    sscanf(buffer, "%d", &(tempStruct->thirdField) == 1); // line 26

    link = List(&tempStruct);

    array[numberOfStructs] = (*tempStructs);
    numberOfStructs += 1;

    array = reAllocArray(&array, &allocSize, numberOfstructs);
}
while(link->newStruct != NULL);

printList(&array, numberOfStructs);
freeArray(&array, numberOfStructs);
}

I get warnings as follows

/main.c:26: warning: comparison between pointer and integer warning: passing argument 1 of 'List' from incompatible pointer type

I get a few of the "passing argument 1" error messages.

What am I doing wrong with these pointers?

Thanks

Upvotes: 0

Views: 143

Answers (4)

Luka Rahne
Luka Rahne

Reputation: 10447

Album *tempStruct;
fgets(&(*tempStruct->firstField),MAXCHARACTERS, stdin);

tempStruct is just a pointer and you should not store anything on this pointer offset

&(*tempStruct->firstField) // or just tempStruct->firstField since &* is just cancellation

I am not sure how this code works but from my knowledge i can see that each line using tempStruct is access violation without exception at

link = List(&tempStruct);

and

&(tempStruct->thirdField) == 1

Will be most likley FALSE in all cases since it is just pointer which can be 1 just by accident.

Upvotes: 1

Jens Gustedt
Jens Gustedt

Reputation: 78903

Probably you just copied code that also does test the return of scanf against 1? Then you have gotten your () wrong. And effectively you should put that into an if clause and test for success.

Upvotes: 0

Natan Yellin
Natan Yellin

Reputation: 6387

You are missing multiple type definitions.

For example, numberOfStructs = 0; should be int numberOfStructs = 0; The same applies to allocSize, array, and datalink.

If you only posted snippets of your code and your original code does not have those problems, then please let us know which line of code causing the error. The line numbers are likely incorrect.

Upvotes: 0

rano
rano

Reputation: 5666

It seems to me that you are misusing sscanf, the third parameter you are passing to it is the logical result from a comparison between an address and the number 1. What are you trying to accomplish there?

Upvotes: 2

Related Questions