Pointer in struct?

This is a discussion on Pointer in struct? within the C Programming forums, part of the General Programming Boards category; Excuse me, I want to ask please. Sorry if my english is bad. I have a problem, my lecturer wanted ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Pointer in struct?

    Excuse me, I want to ask please. Sorry if my english is bad.

    I have a problem, my lecturer wanted me to make a program which consists only pointer and malloc.

    I have a problem in making variable in struct to pointer.

    I mean like this

    Code:
    struct xampl {
      int x;
    };
    what my lecturer want is

    Code:
    struct xampl {
      int *x;
    };
    He said that i can use typedef, but even I tried so many times. I still failed.

    Is anyone know how to solve this problem?
    Last edited by LeonLanford; 04-02-2007 at 06:46 AM.

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Well, what's the difference between this one and a normal pointer ? NOTHING

    Just initialize a struct xampl and then do what you always do:

    Code:
        struct xampl myStruct;
        myStruct.x = malloc (sizeof(int));
        *(myStruct.x) = 123;

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Sorry my computer was broken down so I cannot reply.

    Thanks KONI! it's working

    but I still confuse what is the difference between

    Code:
    *(myStruct.x) = 123;
    and

    Code:
    *(myStruct).x = 123;
    because both of it worked

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'myStruct' isn't a pointer, so you don't dereference it. You dereference the pointer it contains.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Edited out until I get some coffee or something....

    [AnotherEdit]

    OK, another try. Don't know why I didn't realize that . has higher precedence than *. Obviously, that's why we have ->.

    Overall, this means that both expressions in this case mean the same thing. Both of them take myStruct, then grab its member x (due to the .), dereference it (due to the *), and use that as the lvalue for the assignment.

    For whatever reason I got it backwards. I suppose lack of sleep does that to people.

    [/AnotherEdit]
    Last edited by MacGyver; 04-09-2007 at 05:21 AM.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    I'm getting more confused now..

    so what is the right one?

    both of it worked, I've tested it

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you didn't tell how you declared myStruct.
    true with a cast you can make both "work" but only one is right.
    Show the full code.
    Kurt

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    The code is like this

    Code:
    #include <stdio.h>
    
    struct data
    {
       int *bil;
    } ;
    
    int main()
    {
       struct data *tdata;
       
        tdata = (struct data *) malloc (sizeof(struct data *));
    
        tdata.bil = (int *) malloc (sizeof(int));
    
        *(tdata).bil = 123;
    
        printf("%d", *(tdata).bil);    
    
    system("pause");
    return 0;
    }

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    That code doesn't compile for me.

    1) Missing stdlib.h.
    2) Improper assignments with pointer types and all that stuff.

    The notations we are discussing are valid provided that the struct variable you're dealing with is not a pointer. This means tdata should be of type struct data, not a pointer to struct data.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    I'm using devc++ 4.9.9.2

    I usually don't put stlib because it does run in devc.

    My lecturer said that it's pointer to struct, I'm getting more confused now..

  11. #11
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    in that case
    Code:
    tdata->bil = (int *) malloc (sizeof(int));
    *((*tdata).bil) = 123;
    or
    Code:
    (*tdata).bil = (int *) malloc (sizeof(int));
    *(tdata->bil) = 123;
    would be right.
    There shouldn't be a cast of malloc' s return value.
    Without that the compiler would have warned you about the mistakes.
    Kurt

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Altering your code slightly.... This will compile:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct data
    {
    	int *bil;
    } ;
    
    int main()
    {
    	struct data  *tdata;
    	
    	tdata = (struct data *)malloc(sizeof(*tdata));
    	tdata->bil = (int *)malloc(sizeof(int));
    	
    	*tdata->bil = 123;
    	
    	printf("%d\n", *tdata->bil);
    	
    	system("pause");
    	return 0;
    }

  13. #13
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by LeonLanford View Post
    I usually don't put stlib because it does run in devc.
    If you don't include stdlib.h there is no malloc defined.
    That "works" only on a platform where pointers and int's have the same size but the compiler will give you wrong diagnostics.
    Kurt

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    hmm..

    zuk says

    Code:
    *(tdata->bil) = 123;
    and mac says

    Code:
    *tdata->bil = 123;
    so both is right? sorry i cannot test the code because i'm now in internet cafe

  15. #15
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yes both is right. Check out operator precedence.
    Kurt

Page 1 of 2 12 LastLast
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, 10: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, 08: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, 05:58 PM

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