![]() |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 2
| Pointer to struct 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 = Α
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 | |
| | #2 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Hi Paul. Looks like you have some "brushing up" to do! Quote:
*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 | |
| | #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 | |
| | #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 | |
| | #5 |
| Deathray Engineer 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' 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'
__________________ |
| MacGyver is offline | |
![]() |
| Tags |
| pointers |
| Thread Tools | |
| Display Modes | |
|
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 |