A struct member as a pointer to a variable

This is a discussion on A struct member as a pointer to a variable within the C Programming forums, part of the General Programming Boards category; Dear C experts, First and foremost, please let me thank you for all your help in this forum. You are ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    31

    A struct member as a pointer to a variable

    Dear C experts,

    First and foremost, please let me thank you for all your help in this forum. You are making my coding life much, much more pleasant :-)

    Now, to the point. I need to create a struct in which member points to another (global) variable. As I am to pass this struct to functions, and pointers to pointers make me nervous, I thought of checking with the experts first.

    Let me explain myself with a silly example. I know that if I define a function like this:

    Code:
    void myfunc(double *varvalue)
    {
        *varvalue = somevalue;
    }
    and then I call it as

    Code:
    myfunc(&othervariable);
    then the global variable "othervariable" will get the value "somevalue". (I hope I am correct here).

    Well, now I need to do that with structs. My guess (which could be totally wrong) is that I should do the following:

    Code:
    typedef struct _sType 
    {
        double *value
    } sType;
    
    sType mystruct;
    Then, I would initialize it like this?

    from main:
    Code:
    *mystruct.value = &othervariable
    from a function:
    Code:
    void initfunc(sType *s)
    {
        *s->value = &othervariable
    }

    Now, to update the value of "othervariable" should I do this?

    from main:
    Code:
    *mystruct.value = somevalue;
    and from within functions:
    Code:
    void myfunc(sType *s)
    {
        *s->value = somevalue
    }
    Please let me now whether any (or all) of my assumptions are wrong.

    Thanks so much, as usual, for your help

    mc61

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by mc61 View Post
    Well, now I need to do that with structs. My guess (which could be totally wrong) is that I should do the following:

    Code:
    typedef struct _sType 
    {
        double *value
    } sType;
    
    sType mystruct;
    Then, I would initialize it like this?

    from main:
    Code:
    *mystruct.value = &othervariable
    Nope.
    You need to do
    Code:
    (*mystruct).value = &othervariable
    Or just
    Code:
    mystruct->value = &othervariable
    from a function:
    Code:
    void initfunc(sType *s)
    {
        *s->value = &othervariable
    }
    Nope. This would derefence the pointer itself, so you'll probably get a seg fault.
    Just
    Code:
    s->value = &othervariable
    Will do.

    Now, to update the value of "othervariable" should I do this?

    from main:
    Code:
    *mystruct.value = somevalue;
    Well, if mystruct is a pointer, then it's wrong, because you're only dereferencing the pointer to the struct and not the pointer inside the struct.
    If mystruct is a struct on the stack, then yes, it is correct.

    Also, it might be preferable to do
    Code:
    *(s->value) = somevalue
    instead of
    Code:
    *s->value = somevalue
    To make it apparent that you are dereferncing the struct first and then the char* member.

    The rest should be correct, I believe.
    Also, what's the point of global variables if you pass pointers in structs?
    Last edited by Elysia; 01-27-2008 at 04:20 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    31
    Thank you Elysia, I will try what you told me as soon as I can.

    To answer your question about why using global variables, I am implementing sliders and buttons on an OpenGL program, so this is in order that I can create a slider that would control the value of a variable in my model. Everything works for me now, except this last step that you kindly explained to me.

    In case you are wondering why I am doing my own sliders, the vast majority of OpenGL GUIs out there are in C++, which I do not know, and the only one I found written in C (AntTweakBar) does not compile under OSX, which is what I use.

    Thanks again for your help,

    Marcelo

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Well, you could learn C++
    Get on with the times, since it seems all the tutorials are written in C++ these days
    Haha. Anyway, good luck on your project.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    31
    Tell me about those OpenGL C++ tutorials... I somehow managed to learn from them, but I guess I am too old to learn new tricks

    At any rate, your advise worked wonderfully. Here is what I did:

    Code:
    typedef struct _sliderType 
    {
        (other stuff)
        double *variable;       // pointer to variable controlled by slider
    } sliderType;
    Code:
    void create_slider((other stuff),double *var, sliderType *s)
    {
        ...
        s->variable   = var;
    }
    Code:
    void update_slider(sliderType *s) 
    {  
       ... 
       *(s->variable) = s->value;
    }

    and I create the slider with this call:

    create_slider( (other stuff), &somevariable, &myslider);

    Thank you very much Elysia, you certainly made my day

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by mc61 View Post
    Tell me about those OpenGL C++ tutorials... I somehow managed to learn from them, but I guess I am too old to learn new tricks
    I was just echoing what you mentioned. Sarcasm

    Thank you very much Elysia, you certainly made my day
    Not a problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to struct
    By Paul Johnston in forum C Programming
    Replies: 4
    Last Post: 06-11-2009, 03:01 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. pointer to a struct
    By ramdal in forum C Programming
    Replies: 13
    Last Post: 12-15-2005, 08:01 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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