C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-10-2009, 03:24 PM   #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);
}
Paul Johnston is offline   Reply With Quote
Old 06-10-2009, 03:37 PM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 06-10-2009, 03:37 PM   #3
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
That's because ptr1 is pointing to a compound variable type ie a struct instead of to each of its members.
itCbitC is offline   Reply With Quote
Old 06-11-2009, 02:46 AM   #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
Paul Johnston is offline   Reply With Quote
Old 06-11-2009, 03:01 AM   #5
Deathray Engineer
 
MacGyver's Avatar
 
Join Date: Mar 2007
Posts: 3,211
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?
__________________
MacGyver is offline   Reply With Quote
Reply

Tags
pointers

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:57 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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