开发者

Pointer troubles

开发者 https://www.devze.com 2023-02-10 00:25 出处:网络
I have an issue with the following code. int main (int argc, const char * argv[]) { #define MAXCHARACTERS 10

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);
    fg开发者_JS百科ets(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


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?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消