Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

error: ‘ii’ undeclared here (not in a function)

+1 vote
asked Mar 2 by Ashok Meti (130 points)

Hi Folks,

Am I missing something here?

During compilation i am seeing error as "error: ‘ii’ undeclared here (not in a function)"

#include <stdio.h>

typedef struct map_s {

int ii;

int pi;

} map_t;

static map_t map_test[] = {{ii=10, pi =0}, {ii=20, pi=0}};

int main()

{

int i, j = 10;

while (j != 0) {

for (i = 0; sizeof (map_test)/sizeof (*map_test); i++) {

map_test[i].pi++;

}

j--;

}

for (i = 0; sizeof (map_test)/sizeof (*map_test); i++) {

printf("i %d, pi %d\n", map_test[i].ii, map_test[i].pi);

}

return (0);

}

1 Answer

+1 vote
answered Mar 5 by Peter Minarik (55,760 points)

The problem with your code is twofold.

First of all, when you initialize your map_test array, you need to use the right syntax and must not include the name of the fields, but assign them in the correct order.

Second, in your loop, you should check if the loop variable is less than the number of elements in the map_test array as your code right now only checks if there is any element in the array.

#include <stdio.h>

typedef struct map_s
{
    int ii;
    int pi;
} map_t;

static map_t map_test[] = { { 10, 0 }, { 20, 0 } };

int main()
{
    int i, j = 10;
    while (j != 0) {
        for (i = 0; i < sizeof(map_test)/sizeof(*map_test); i++) {
            map_test[i].pi++;
        }
        j--;
    }
    
    for (i = 0; i < sizeof (map_test)/sizeof(*map_test); i++) {
        printf("i %d, pi %d\n", map_test[i].ii, map_test[i].pi);
    }
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...