Please be patient as I am a confused beginner :confused:
I have been working through the excellent C beginners' tutorial on this site and have thoroughly enjoyed it, but I seem to have become stuck on lesson 7, Structures. The problem is that I cannot get the example program to compile either in Visual C++ Express 2005 nor MinGW Developer Studio. With both compilers I get a series of six errors basically complaining about undeclared identifiers in relation to the newly created structs. At first I thought I must have miskeyed the code, but even a straight copy and paste spews the same errors. For convenience I've posted the short code below and I'd be much obliged if anyone can spot my mistake.

Thanks in advance, Steve

Code:

#include <stdio.h>

struct xampl {
int x;
};

int main()
{
xampl structure;
xampl *ptr;

structure.x = 12;
ptr = &structure; /* Yes, you need the & when dealing with
structures and using pointers to them*/
printf( "%d\n", ptr->x ); /* The -> acts somewhat like the * when
does when it is used with pointers
It says, get whatever is at that memory
address Not "get what that memory address
is"*/
getchar();
}

11-21-2005

SlyMaelstrom

In C, I believe you're supposed to put struct infront of every declaration of that struture. This is not the case in C++. It was just a poor port to C from the C++ tutorial, I guess. Maybe someone else has a better explaination.

This will compile, though

Code:

#include <stdio.h>

struct xampl {
int x;
};

int main()
{
struct xampl structure;
struct xampl *ptr;

structure.x = 12;
ptr = &structure; /* Yes, you need the & when dealing with
structures and using pointers to them*/
printf( "%d\n", ptr->x ); /* The -> acts somewhat like the * when
does when it is used with pointers
It says, get whatever is at that memory
address Not "get what that memory address
is"*/
getchar();
}

11-21-2005

itsme86

Sly nailed it. C requires the struct tag before each declaration.

11-21-2005

Steve MacD

Quote:

Originally Posted by SlyMaelstrom

In C, I believe you're supposed to put struct infront of every declaration of that struture. This is not the case in C++.

Thanks a million Sly and itsme86, that nailed it. Thanks again for the quick response. :D