Thread: Pointer to struct

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Pointer to struct

    Hi

    Revisiting c programming after oh ages (15 years :-() and writing a pointer to a struct.
    The program at the bottom gives me

    Struct Alpha using values is 9, 7, 2.434000:
    Address of pointer ptr1 is 134510852
    Size of struct is 12
    Values are 9 , 7 and 0.000000
    Using -> notation x is 9
    Using -> notation y is 7
    Using -> notation depth is 2.434000



    However I cannot see for the life of me why
    printf("Values are %d , %d and %f\n",*ptr1, *ptr1, *ptr1);

    gives
    Values are 9 , 7 and 0.000000

    Why does the same reference (right term?) give different results and more specifically the first two values in the instance of my struct ?

    TIA Paul

    Code:
    /* 
     * File:   newmain.c
     * Author: paulj
     *
     * Created on 10 June 2009, 20:11
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char** argv) {
    
        struct Position {
            int x;
            int y;
            float depth;
        };
    
        struct Position Alpha;
            Alpha.x = 9;
            Alpha.y = 7;
            Alpha.depth = 2.434;
        ;
        struct Position * ptr1;
        ptr1 = &Alpha;
    
        printf("Struct Alpha using values is %d, %d, %f:\n", Alpha.x, Alpha.y, Alpha.depth);
        printf("Address of pointer ptr1 is %d\n",ptr1);
        printf("Size of struct is %d\n",sizeof(Alpha));
        printf("Values are %d , %d and %f\n",*ptr1, *ptr1, *ptr1);
        
      
    
        printf("Using -> notation x is %d\n",ptr1->x);
        printf("Using -> notation y is %d\n",ptr1->y);
        printf("Using -> notation depth is %f\n",ptr1->depth);
    
        return (EXIT_SUCCESS);
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hi Paul. Looks like you have some "brushing up" to do!

    Quote Originally Posted by Paul Johnston View Post
    However I cannot see for the life of me why
    printf("Values are %d , %d and %f\n",*ptr1, *ptr1, *ptr1);

    gives
    Values are 9 , 7 and 0.000000
    Actually, it says "Values are 9 , 9 and 0.000000" (I did test it). That is because *ptr1, *ptr1, and *ptr1 are all the same thing, where as ptr1->x, ptr1->y, and ptr1->depth are three different things.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    That's because ptr1 is pointing to a compound variable type ie a struct instead of to each of its members.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Test

    I retried it on a second machine (Open Solaris 0609 using NetBeans 6.5.1 ) and it does return
    Values are 9 , 7 and 0.0000

    Cutting and pasting into NetBeans on windows does the same as does manually compiling with gcc 3.4.3 on a Unix box.
    Guess I need to look into compound variable types !

    Regards Paul

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Compiling as C90:

    Code:
    : In function `main':
    :24: warning: ISO C90 forbids mixed declarations and code
    :28: warning: int format, pointer arg (arg 2)
    :30: warning: int format, Position arg (arg 2)
    :30: warning: int format, Position arg (arg 3)
    :30: warning: double format, Position arg (arg 4)
    : At top level:
    :11: warning: unused parameter 'argc'
    :11: warning: unused parameter 'argv'
    Compiling as C99:

    Code:
    : In function `main':
    :28: warning: int format, pointer arg (arg 2)
    :30: warning: int format, Position arg (arg 2)
    :30: warning: int format, Position arg (arg 3)
    :30: warning: double format, Position arg (arg 4)
    : At top level:
    :11: warning: unused parameter 'argc'
    :11: warning: unused parameter 'argv'
    In other words, your program is technically broken with your pointer usage. Arguing about what gets printed is kind of pointless. Why are you deferencing a pointer to a struct and giving that to printf as two ints and a float? Are you trying some form of digitial suicide?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. "dereferencing pointer to incomplete type"
    By incognito54 in forum C Programming
    Replies: 2
    Last Post: 11-01-2005, 09:50 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

Tags for this Thread