1

I am trying to get familiar with struct and pointers in C and I am running into a bunch of syntax errors like "missing ';' before type", "missing ')' before type" and "undeclared identifier: 'i'". Everything seems fine, I know i is declared and I don't seem to be missing any ; or ).

#include <stdlib.h>
#include <stdio.h>

#pragma warning(disable: 4996)
struct Room; 
struct House;

struct Room 
{
    float width;
    float length;
    float height;
    char *name;
};

struct House
{
    char *address;
    struct Room *rooms[10]; 
};

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

    struct House h;
    h.address = "10 Palace Road";  
    for(int i = 0; i < 10; i++) // 6 errors occur here
    {
        h.rooms[i] = NULL;
    }
    struct Room hall;
    hall.width = 10;
    hall.length = 12;
    hall.height = 9;
    hall.name = "Hall";

    h.rooms[0] = &hall;
    printHouse(h);
    system("PAUSE");
    return 0;

}

void printHouse(struct House house)
{

    printf(house.address);
    printf("\n\n\n");

    for (int i=0; i<10; i++)
    {
        if (house.rooms[i] != NULL)
        {
            struct Room r = *house.rooms[i];
            printf("Room # %d: %s", i+1, r.name);
        }
    }
}
4
  • declare i outside of for. in some C compilers it doesnt work inside the for Commented Feb 21, 2014 at 0:33
  • struct Room hall; is a local variable. It will be reused on each iteration of the loop. Commented Feb 21, 2014 at 0:53
  • And are we to guess which compiler you're using? Commented Feb 21, 2014 at 0:56
  • I resolved the issue, for some reason my source file was a .c file. I changed it to .cpp and everything compiled. Thanks for the feedback everyone! P.S. I am using Visual Studio 2010 Commented Feb 21, 2014 at 2:10

3 Answers 3

4
printf(house.address);

should be

printf("%s",house.address);

Also you must declare your function printhouse, since you have defined it after main.

#include <stdlib.h>
#include <stdio.h>
#pragma warning(disable: 4996)
struct Room; //you don't need this

**EDIT**
struct House
{
char *address;
struct Room *rooms[10];
};
void printHouse(struct House house);

Declare House first then the function.

Sign up to request clarification or add additional context in comments.

Comments

3
int i;
for (i = 0; i < 10; i++){
    //...
}

In earlier versions of C, you cannot declare I inside a loop.

2 Comments

Really? Perhaps you mean that in versions of the C language older than C99, declarations must be at the beginning of a block?
@ElchononEdelson: You're talking about two distinct C99 features: mixing declarations and statements within a block, and declarations in for loop headers.
2

Some versions of C compilers do not allow 'i' to be declared in the loop. Try declaring 'i' separately at the beginning of 'main()'. That should work.

1 Comment

@Will: The use of system("PAUSE"); suggests that the OP is likely to be using a Microsoft compiler. Microsoft does not support C99.

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.