Yasin
Yasin

Reputation: 3

C initializing array of structure error

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
}

Upvotes: 0

Views: 103

Answers (4)

user3629249
user3629249

Reputation: 16540

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

Upvotes: 0

chqrlie
chqrlie

Reputation: 145297

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;
}

Upvotes: 4

qwertz
qwertz

Reputation: 14792

  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 *.

Upvotes: 0

haccks
haccks

Reputation: 106112

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]);

Upvotes: 1

Related Questions