Pointer inside a struct - how to access it?

This is a discussion on Pointer inside a struct - how to access it? within the C Programming forums, part of the General Programming Boards category; I have a struct like this: Code: struct Person { double number; int age, *exampoint; }Prsn[50]; I want to acces ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    35

    Pointer inside a struct - how to access it?

    I have a struct like this:
    Code:
    struct Person
    {
      double number;
      int age, *exampoint;
    }Prsn[50];
    I want to acces the pointer defined inside the struct later. How can I call it? I tried this, which works with non-pointer variables. But it gives a parse error.
    Code:
    Prsn[1].*exampoint=&a[1]
    Normally I call something inside my array of struct by writing the struct's name, a dot and the required variable. But how can I get the pointer? Please tell me if I'm making a logical mistake in thinking.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    There is better ways to go about making an array of structs??
    Last edited by camel-man; 10-14-2011 at 03:13 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    If you have a struct element that is a pointer, and you need to dereference that pointer, you need something like:
    Code:
    *Prsn[1].exampoint = &a[1];
    though I prefer explicit parentheses so it's clear what you're trying to do:
    Code:
    *(Prsn[1].exampoint) = &a[1];

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by camel-man View Post
    prsn->exampoint???

    Also There is better ways to go about making an array of structs
    I've tried that but my program stops running when I do that :/

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by anduril462 View Post
    If you have a struct element that is a pointer, and you need to dereference that pointer, you need something like:
    Code:
    *Prsn[1].exampoint = &a[1];
    though I prefer explicit parentheses so it's clear what you're trying to do:
    Code:
    *(Prsn[1].exampoint) = &a[1];
    I tried them. Compiler doesn't give an error but it says "warning:assignment makes integer from pointer without a cast" How can I prevent it? And thanks

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    try taking off the *
    ex. Prsn[1].exampoint = &a[1];

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    Doh! I saw the * in your first example and thought you wanted to dereference, but I should have known because the & in &a[1] is an address. In English, do you want Prsn[1].exampoint to point to element #1 of a? If so, then you need to drop the * from my examples.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by camel-man View Post
    try taking off the *
    ex. Prsn[1].exampoint = &a[1];
    Quote Originally Posted by anduril462 View Post
    Doh! I saw the * in your first example and thought you wanted to dereference, but I should have known because the & in &a[1] is an address. In English, do you want Prsn[1].exampoint to point to element #1 of a? If so, then you need to drop the * from my examples.
    Yes I removed the * .Program seems to run well at first but stops running at some time. Maybe it's not caused by the struct or the pointer. I'll check my entire syntax now.

    By the way, if I remove the array part and just leave one single struct, how can i refer to the pointer? e.g. the struct is;
    Code:
    struct Person
    {
      double number;
      int age, *exampoint;
    };

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    Code:
    struct Person
    {
      double number;
      int age, *exampoint;
    };
    That just defines the struct, it doesn't define any variables. If you don't want an array, take the [ ] out of the definition and wherever else you use it. Something like:
    Code:
    // define the struct
    struct Person
    {
        double number;
        int age;
        int *exampoint;
    };
    
    // define a variable of type 'struct Person'
    struct Person that_guy;
    
    that_guy.number = 123.45;
    that_guy.age = 42;
    that_guy.exampoint = &a[1];

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by anduril462 View Post
    Code:
    struct Person
    {
      double number;
      int age, *exampoint;
    };
    That just defines the struct, it doesn't define any variables. If you don't want an array, take the [ ] out of the definition and wherever else you use it. Something like:
    Code:
    // define the struct
    struct Person
    {
        double number;
        int age;
        int *exampoint;
    };
    
    // define a variable of type 'struct Person'
    struct Person that_guy;
    
    that_guy.number = 123.45;
    that_guy.age = 42;
    that_guy.exampoint = &a[1];
    Thanks a LOT!!!

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually... pointers inside structs are usually some pretty bad juju....

    If you aren't careful --really careful-- you're likely to end up with all your pointers pointing to the same place or, even worse, all those pointers aimed at values that have gone out of scope. As a rule it's better to simply put the data right in the struct, instead of pointing to it.

    And, in this case... since an int and a pointer are the same size on a 32 bit machine, you definatly aren't saving any space. On a 64 bit machine where pointers are 8 bytes, your struct actually ends up being bigger.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > Actually... pointers inside structs are usually some pretty bad juju....
    Oh come on - how are you supposed to create things like linked lists then?

    Pointers inside structs have the same problems (and benefits) as pointers outside of structs. Of course you've got to make sure where the pointer points to, that's no different from before.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem View Post
    > Actually... pointers inside structs are usually some pretty bad juju....
    Oh come on - how are you supposed to create things like linked lists then?
    Code:
    struct linkedlist
    {
        struct linkedlist next;
        int data;
    };
    Like that? I don't even have to hook up the links that way!


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

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Good luck trying to compile that on a finite machine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > Actually... pointers inside structs are usually some pretty bad juju....
    Oh come on - how are you supposed to create things like linked lists then?

    Pointers inside structs have the same problems (and benefits) as pointers outside of structs. Of course you've got to make sure where the pointer points to, that's no different from before.
    Ok, maybe I should have said "pointers to data"... The exception being the next/prev pointers in linked lists (which I avoid like the plague)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. Replies: 2
    Last Post: 12-06-2010, 12:28 PM
  3. Pointer to union inside struct
    By lv2eof in forum C Programming
    Replies: 4
    Last Post: 05-28-2010, 06:55 PM
  4. referencing FILE pointer inside struct
    By Cquest in forum C Programming
    Replies: 3
    Last Post: 03-22-2008, 11:53 AM
  5. Problem with DMA inside a struct pointer
    By Nakor in forum C Programming
    Replies: 5
    Last Post: 09-15-2007, 08:14 PM

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