0

I'm trying to initialize int array in struct but when I take a value from scanf then access to values it's give me warning: format specifies type 'int' but the argument has type 'int *' [-Wformat] error. This is the my code:

struct maclar {
    int macNo[40];
    int evSahibi[40];
    int deplasman[40];
} mac[40];

void verileriAl(FILE *mp) {
    for (int i = 0; fscanf(mp,"%d %d %d",
                           mac[i].macNo, mac[i].evSahibi, mac[i].deplasman) != -1; i++) {
        ........codes here .....
    }
}

main() {
    FILE *mp = fopen("maclar.txt", "r");
    verileriAl(mp);
    printf("%d\n", mac[0].macNo);  //give me warning and wrong value
}
4
  • in general, the definition of a struct should be kept separate from the declaration of the struct instance. Commented Dec 27, 2015 at 18:35
  • when calling any of the scanf family of fucntions, check for (in this case) 3. Any other returned value means an error occurred Commented Dec 27, 2015 at 18:38
  • the function: main() when run on an OS, only has 2 valid and one optional signature. All those signatures have a int return type. With out a OS, the only valid return type is void Commented Dec 27, 2015 at 18:40
  • when calling fopen() always check (!=NULL) the returned value to assure the operation was successful. Commented Dec 27, 2015 at 18:42

4 Answers 4

4

You are passing a array of int to printf for the %d format, hence the format mismatch. It is a good thing you compile with appropriate warnings, otherwise this error would go unnoticed.

Why do you make your structure hold arrays of 40 values for each member?

This is probably an error, out of confusion.

Fix your code this way:

struct maclar {
    int macNo;
    int evSahibi;
    int deplasman;
} mac[40];

int verileriAl(FILE *mp) {
    int i;

    if (mp == NULL)
        return -1;
    for (i = 0; i < 40 && fscanf(mp,"%d %d %d", 
            &mac[i].macNo, &mac[i].evSahibi, &mac[i].deplasman) == 3; i++) {
        continue;
    }
    return i;
}

int main(void) {
    FILE *mp = fopen("maclar.txt", "r");
    if (mp != NULL) {
        if (verileriAl(mp) > 0)
            printf("%d\n", mac[0].macNo);
        fclose(mp);
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Yasin This answer's change to == 3 versus original != -1 is also an important code improvement. Test for the one value you want instead of testing for one of the many values you do not want.
1

The reason is that you are passing mac[i].macNo which converts to int * type. %d expects int type argument.

Also note that you are doing the same mistake in fscanf. One of the possible solution is to declare mac as

struct maclar
{
    int macNo[40];
    int evSahibi[40];
    int deplasman[40];
}mac;  

Now change for statement to

for (int i = 0; fscanf(mp,"%d %d %d",mac.macNo[i],mac.evSahibi[i],mac.deplasman[i]) != -1 ; i++)  

and change printf call to

printf("%d\n", mac.macNo[0]);

4 Comments

@Yasin; Updated the answer.
This does not look like the correct fix... Better make mac an array of structures with 3 int members.
@chqrlie; It is difficult to say without knowing the intent of the program.
@haccks: I agree its difficult, but it seems to make more sense to have an array if structures instead of a structure of arrays... My biassed opinion of course.
0
  1. Because the format %d of scanf reads just one int, but you try to assign this value to an array of ints. You cannot put a value into an array without specifying the index.*

    edit: Actually the value will just be put into the first index as because int * is evaluated.

  2. As haccks answered: The real problem is printf, because it expects an argument of type int rather than int *.

2 Comments

That's true, but not the reason.
it is fscanf() and as I know that it's available
0

regarding this line:

printf("%d\n", mac[0].macNo);

the field macNo is an array of int

In C, a reference to the array name degrades to the address of the first byte of the array.

So this: mac[0].macNo results in an address.

the format specifier: %d will not properly handle an address.

suggest:

printf("%p\n", mac[0].macNo);

because %p is specifically for printing an address

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.