views:

121

answers:

4
#include <stdio.h>

typedef struct point{
    int x; 
    int y;
};

void main (void){

    struct point pt;
    pt.x = 20;
    pt.y = 333;

    struct point pt2;
    pt2.y = 55;

    printf("asd");
    return;
}

VS 2008

c:\documents and settings\lyd\mis documentos\ejercicio1.c\ejercicio1.c\ejercicio1.c(14) : error C2143: syntax error : missing ';' before 'type'
c:\documents and settings\lyd\mis documentos\ejercicio1.c\ejercicio1.c\ejercicio1.c(15) : error C2065: 'pt2' : undeclared identifier
c:\documents and settings\lyd\mis documentos\ejercicio1.c\ejercicio1.c\ejercicio1.c(15) : error C2224: left of '.y' must have struct/union type
Build log was saved at "file://c:\Documents and Settings\LYD\Mis documentos\ejercicio1.c\ejercicio1.c\Debug\BuildLog.htm"
ejercicio1.c - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
+3  A: 

Remove the word typedef.

R..
It is still not compiling after removing the typedef
DanC
AFter removing typedef: (14) : error C2143: syntax error : missing ';' before 'type'
DanC
Move the variable declarations of `pt` and `pt2` before any statements. MSVC does not support C99.
R..
That is, put them both at the beginning of the `main` function (but still inside it).
R..
+3  A: 

It compiles just fine on my gcc 4.4.3.

However, you are trying to define a new type:

typedef struct point{
    int x; 
    int y;
};

but it seems you forgot to name this new type (I'll just call it point_t):

typedef struct point{
    int x; 
    int y;
} point_t;

Later, on your code, you could use it like:

point_t pt;
pt.x = 20;
pt.y = 333;
karlphillip
`point_t` is a bad idea for a type name, since `*_t` is reserved by POSIX.
R..
GCC 4.4.3 supports C99; MSVC 2008 does not.
Jonathan Leffler
GCC will give `warning: ISO C90 forbids mixed declarations and code`.
jleedev
@R Thank you for reminding us of that. I'm sure DanC will name it according to his likes.
karlphillip
A: 

Try moving the declaration of pt2 to the top of the function. Some C compilers require declarations to either be global or at the start of a code block.

torak
+3  A: 

Since the question is tagged C (and not C++), and since the compiler is MSVC 2008, you are stuck with C89 semantics. That means you cannot declare variables in a block after the first statement. Hence, the second struct variable is not allowed there. (Both C99 and C++ allow you declare variables at any point in the block. Go tell MS to update their C compiler to support C99.)

Your other bug is that main() returns an int, hence:

#include <stdio.h>

struct point
{
    int x; 
    int y;
};

int main (void)
{
    struct point pt;
    struct point pt2;
    pt.x = 20;
    pt.y = 333;
    pt2.x = 4;
    pt2.y = 55;
    printf("asd");
    return 0;
}

Some hours later: the keyword typedef is not needed in the code because no name is specified after the close brace and before the semi-colon. This doesn't stop it compiling; it will elicit a warning with the compiler set fussy.

Jonathan Leffler
Yes, moving the declarations to the top made it work. Thanks!
DanC