Thread: Structures tutorial problem

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    UK
    Posts
    6

    Structures tutorial problem

    Hi everyone,

    Please be patient as I am a confused beginner
    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();
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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();
    }
    Sent from my iPadŽ

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Sly nailed it. C requires the struct tag before each declaration.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Location
    UK
    Posts
    6
    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.

    Cheers, Steve

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with structures
    By dereach in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 08:22 AM
  2. Saving vectors of structures problem
    By redeck in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2004, 04:47 PM
  3. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  4. Problem running a Cboard C++ tutorial!
    By Monkey Liar in forum C++ Programming
    Replies: 6
    Last Post: 02-15-2002, 03:32 AM
  5. problem with structures
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2002, 02:12 PM