Creating a Pointer to a Structure

This is a discussion on Creating a Pointer to a Structure within the C Programming forums, part of the General Programming Boards category; I got back from Prelude's corner reviewing his tutorials on "Pointers" and another one on "Structures." I am still stumped ...

  1. #1
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Unhappy Creating a Pointer to a Structure

    I got back from Prelude's corner reviewing his tutorials on "Pointers" and another one on "Structures." I am still stumped on a problem - I'm using the exact statements and format found in Sams "Teach yourself C in 21 days...6th edition" on the section dealing with structures.

    I'm using MS VS 2010, which may be part of the problem. Please only address the problem at hand - I could care less if the code below creates local or global variables, etc. I have not completed main() because of an earlier problem.

    Here's my simple code:

    Code:
    // dummy test area to test small snips of C code.   
    // 8/9/10
    //
    
    #include <stdio.h>
    
    struct Part		// define a simple structure
    {
    	short number;
    	char name[10];
    };
    
    struct Part *p_part; 	// declare a pointer to type part
    struct Part gizmo;	// declare an instance of type part
    
    p_part = &gizmo;        // now initialize the pointer
    
    
    int main(void)
    {
    
    (*p_part).number = 100;
    
    
     return 0; 
    }
    And here is the offending statement per my compiler:

    Code:
    p_part = &gizmo;    // now initialize the pointer
    I'm getting "Error: this declaration has no storage class or type specifier."

    Grrrr.....

    If it needed a storage class of type specifier, the book should have told me
    to use one.

    Thanks for any help!

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Try moving this into main:

    Code:
    p_part = &gizmo;        // now initialize the pointer

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You cannot have a command (like assignment) outside a function.
    That is why you put it in the main function.
    The only thing you can do is initialize variables, which is not exactly a command even though they have the same syntax.

    The compiler assumes that you wanted to declare a variable by that line of code and gives you the error it gives you

  4. #4
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile Wow, that was fast!

    Thank you very much kermit and C_nutra.

    I learned something new; you can initialize variables outside of main(), but I cannot make an assignment statement. From a beginner's view, this point is subtle. But it's starting to make sense. I compiled the code with the correction & all is well.

    You guys are awesome! :-)

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    Btw, you are aware of the -> syntax?
    Such as p_part->number = 100;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Hi Elysia, Yes I knew that and that was how I ultimately printed out the structure's data. A few pages later in this book states: "If str is a structure, p_str is a pointer to str, and memb is a member of str, then one can access str.memb by writing p_str -> memb"

    also it says the following 3 expressions will print out the same data:

    Code:
    str.memb
    
    (*p_str).memb
    
    p_str -> memb
    Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 05:35 PM
  5. Pointer to next Structure
    By Garfield in forum C Programming
    Replies: 6
    Last Post: 09-16-2001, 03:18 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21