Thread: Pointer to Structure error explanation

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    21

    Pointer to Structure error explanation

    I have modified some tutorial code to get input from the keyboard, and then call a function with a pointer to a structure as input.

    I have got it working but I don't understand why the first time I tried it it didn't work, it only worked when I used & in scanf for the integer of age.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct tag{                     /* the structure type */
        char lname[20];             /* last name */
        char fname[20];             /* first name */
        int age;                    /* age */
        float rate;                 /* e.g. 12.75 per hour */
    };
    
    struct tag my_struct;           /* define the structure */
    void show_name(struct tag *p);  /* function prototype */
    
    int main(void)
    {
        struct tag *st_ptr;         /* a pointer to a structure */
        st_ptr = &my_struct;        /* point the pointer to my_struct */
        
        printf("\n What is your first name?");
        scanf("\n %s", st_ptr->fname);
        printf("\n What is your surname?");
        scanf("\n %s", st_ptr->lname);
        printf("\n What is your age?");
        scanf("\n %d", &my_struct.age);
    
        show_name(st_ptr);          /* pass the pointer */
        /* return 0; */
    }
    
    void show_name(struct tag *p)
    {
        printf("\n %s ", p->fname);  /* p points to a structure */
        printf(" \n %s ", p->lname);
        printf("%d\n", p->age);
    }
    In red, if I try without the & it gives me:

    pointerstruct.c: In function ‘main’:
    pointerstruct.c:24: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    I know for scanf the & is not necessary for fname and lname as they are arrays and have constant pointers to the first element of the array anyway. But with the age I can't see why I get this message when use st_ptr->age, as I am using a pointer to the address at my_struct, which is what the error message is complaining of, it wants an address, but I am giving it an address.

    Also I have seen in tutorials that they use st_ptr->age or my_struct.age without the & in scanf.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why would you? So &my_struct.age, or &st_ptr->age will give you the address of the element age - a pointer to an integer, which is what scanf should have. If you do not use the & operator, you get the integer age passed to scanf - and unless that HAPPENS to be a valid address, it would cause an invalid memory access (segfault for example).

    So, whether you use a structure or pointer to structure, you need to get the ADDRESS of the age element in the structure.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    my_struct.age is an int not a pointer so you need & with it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by hellogamesmaste View Post
    Also I have seen in tutorials that they use st_ptr->age or my_struct.age without the & in scanf.
    Then they are wrong.
    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.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    21
    Is it true then, that whenever your pointer points to a structure address, it does not point to all the components, so you have to use & for age as if it was just a normal variable? otherwise if you leave out the & it points to the value of int rather than the address, so it won't compile?

    Damn tutorials online, maybe I should get a book.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It points to the structure. Using the -> notation follows the pointer to the actual structure itself, so the result is never a pointer (unless the field itself is a pointer).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 06:35 PM
  5. Pointer to next Structure
    By Garfield in forum C Programming
    Replies: 6
    Last Post: 09-16-2001, 03:18 PM