Thread: Pointer and Structure question...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Pointer and Structure question...

    Hey,

    Here's an example of some of my code.

    Code:
    struct ss
    {
         int *a;
    };
    
    DWORD WINAPI thread ( LPVOID *pParam )
    {
         ss *s = reinterpret_cast<ss*>(pParam);
    
         int a = 4;
    
         //None of the following lines work:
         s->a = a;
         (*s).a = a;
    
         return 0;
    }
    How can I set the value for the pointer inside the pointer of the struct?

    Thanks,

    Guitarist809
    ~guitarist809~

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You must set a pointer variable to equal a pointer. So you could do s->a = &a, for instance.

    Edit: You wouldn't want to do that, since "plain" a is local to the function and would therefore cease to exist once you hit the return statement, leaving s->a to point at nothing in particular. But you do need to set a pointer variable equal to a pointer.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That's because those lines are trying to assign an int to an int* variable.
    You need something like this:
    Code:
    s->a = new int;
    *(s->a) = a;
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Thanks for the response!

    What I'm trying to do is modify a variable outside of the thread. Once this thread terminates, the address of the plain variable "a" will disappear, and give an error. Also, the pointer is already set to an address (sorry, forgot to mention this), I just need to change it's value.


    **edit**

    Wow, that was a quick reponse (did it right before I submitted this). Anyway, that did the trick! Thank you so much for the help!
    ~guitarist809~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to structure question
    By matthughes in forum C Programming
    Replies: 8
    Last Post: 05-19-2007, 01:07 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM