Thread: changing void * value in struct

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    86

    changing void * value in struct

    having issues i want to change value in struct with
    void * var;
    how would i do this
    Code:
    typedef struct
    {
    void * var;
    
    }test1;
    main()
    {
    test1.var =1 ;<?
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    #include <stdio.h>
     
    typedef struct
    {
        int inst;
        void * t1;
    } Dip;
     
    int func(Dip *req)
    {
        int response;
     
        if (req->inst == 0)
        {
            response = 0;
        }
        else
        {
            response = 1;
        }
     
        return response;
    }
     
    int main(void)
    {
        int result;
        Dip req;
     
        req.inst = 1;
        result = func(&req);
        printf("inst=%d; result=%d\n", req.inst, result);
     
        //change value of t1
        t1 x;
        &x =1;
    
        req.inst = 0;
        result = func(&req);
        printf("inst=%d; result=%d\n", req.inst, result);
     
        return 0;
    }

    better example

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    prog.c: In function 'main':
    prog.c:29:5: error: unknown type name 't1'
    t1 x;
    ^
    prog.c:30:7: error: lvalue required as left operand of assignment
    &x=1;
    ^

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I recommend that you work through an introductory book on C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    I am that where i got the example from
    any help?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Then you should go through the chapter on pointers again, as well as the chapter on structs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    i got examples from school hw
    im struggling
    :/
    the way you teach is alot better than my professor doesnt even give anynotes or videos or any reference lectuer is just about his life

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    im struggling
    :/
    the way you teach is alot better than my professor doesnt even give anynotes or videos or any reference lectuer is just about his life
    Thanks for the compliment, but unfortunately, your fundamentals are too weak for help to be given piecemeal at this level. You really need to rebuild your fundamentals before you can continue.

    Let's zoom in to the errors:
    • "unknown type name 't1'". Having gotten this far, you should know that this looks like a declaration of an object named x with type t1:
      Code:
      t1 x;
      But the only place where t1 appears before this is as a member of Dip. If you understood this, it should be obvious to you what is wrong.
    • "lvalue required as left operand of assignment". You don't know what this means, and that's reasonable for a beginner. But you should know that &x results in a pointer to x. So, what on earth could this statement mean?
      Code:
      &x =1;

    The fact that you were stumped by these means that you are too weak in your understanding of simple object declarations and understanding of struct membership to tackle this problem. Likewise, you are so clueless about pointers that you couldn't connect the second thing about them to what you wrote! So, the solution is to revise your earlier learning material.

    If indeed your earlier learning material is so bad that this is of no help, then you must seek better learning material, or face the likelihood that you will fail.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Quote Originally Posted by laserlight View Post
    Thanks for the compliment, but unfortunately, your fundamentals are too weak for help to be given piecemeal at this level. You really need to rebuild your fundamentals before you can continue.

    Let's zoom in to the errors:
    • "unknown type name 't1'". Having gotten this far, you should know that this looks like a declaration of an object named x with type t1:
      Code:
      t1 x;
      But the only place where t1 appears before this is as a member of Dip. If you understood this, it should be obvious to you what is wrong.
    • "lvalue required as left operand of assignment". You don't know what this means, and that's reasonable for a beginner. But you should know that &x results in a pointer to x. So, what on earth could this statement mean?
      Code:
      &x =1;






    The fact that you were stumped by these means that you are too weak in your understanding of simple object declarations and understanding of struct membership to tackle this problem. Likewise, you are so clueless about pointers that you couldn't connect the second thing about them to what you wrote! So, the solution is to revise your earlier learning material.

    If indeed your earlier learning material is so bad that this is of no help, then you must seek better learning material, or face the likelihood that you will fail.
    I wont fail because i refuse to.

    i have to type cast t1?
    x->1;?? is this what you mean left operand

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    I wont fail because i refuse to.
    Determination to succeed is only a starting point: by itself, it will not stop you from failing. You need to turn that determination into concrete action to succeed, and I am telling you that one of the most important steps in that direction is to brush up on your fundamentals.

    Quote Originally Posted by amahmoo
    i have to type cast t1?
    No. You have to understand what is t1.

    Quote Originally Posted by amahmoo
    x->1;?? is this what you mean left operand
    No. I could try to explain the error message to you, but it is a futile exercise when you are just stabbing in the dark because you lack the fundamentals.

    I mean, the relevant comment says "change value of t1". What is t1? You effectively answered this question in the thread's title: "changing void * value in struct". Yet, the code that you actually wrote to do that in post #2 has no relation to the struct object named req. The code that you wrote in post #1 displays a complete lack of understanding of struct types, struct objects and pointers. I am not saying this to attack you; I'm saying this because you need to understand the basics before you can handle what you are trying to handle.
    Last edited by laserlight; 11-24-2015 at 12:22 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    #include <stdio.h>
    Code:
    typedef struct
    {
        int inst;
        void * t1;
    } Dip;
    
    int func(Dip *req)
    {
        int response;
    
        if (req->inst == 0)
        {
            response = 0;
        }
        else
        {
            response = 1;
        }
    
        return response;
    }
    
    int main(void)
    {
        int result;
        Dip req;
    
        req.inst = 1;
        result = func(&req);
        printf("inst=%d; result=%d\n", req.inst, result);
    
        //change value of t1
    req->t1 = 1;
        // i know whats going on i just need some clarificcation im just being lazy with writing out code i need to lock it up
    
        req.inst = 0;
        result = func(&req);
        printf("inst=%d; result=%d\n", req.inst, result);
    
        return 0;
    }
    
    but i get a error makes a pointer from integer without cast?
    i am confused on how to type caste in this situation

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why did you write req->t1?

    Why are you trying to assign 1 to whatever it is that you are trying to assign to? As in, why specifically the value 1?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    just because i ask stupid questions doesnt mean i lack all fundamentals its asking the questions where the learning starts if i didn't ask all the questions (stupid questions for you but for me they were need to know) i have i wouldn't have learned all i have
    Last edited by amahmoo; 11-24-2015 at 12:38 PM.

  14. #14
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    i need to assign it to 1 because i want to know how i can assign a void pointer variable in a struct with 1
    or any value i want to understand how i can do this i need to type caste it i just dont know how

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    just because i ask stupid questions doesnt mean i lack fundamentals its asking the questions where the learning starts if i didn't ask all the stupid questions i have i wouldn't have learned all i have from you
    If you worked through proper learning material, you would not be asking questions that are so far off the mark. The learning should start with such fundamentals, not with a bunch of questions posed on a forum.

    Quote Originally Posted by amahmoo
    i need to assign it to 1 because i want to know how i can assign a void pointer variable in a struct with 1
    Compile this program:
    Code:
    int main(void)
    {
        void *p;
        p = 1;
        return 0;
    }
    You will find that it results in a warning message similiar to the one that you encountered. The thing is, assigning a specific integer value -- other than zero -- to a pointer is usually not done, unless you really know what you are doing. You could assign 0 to the pointer and it will be fine:
    Code:
    int main(void)
    {
        void *p;
        p = 0;
        return 0;
    }
    because 0 is a null pointer constant, so you would just be making p be a null pointer, and that's alright. Hence, I ask why you want to assign 1 to the pointer. What is the underlying reason? "Because I want to do so" is a bad answer: my reply would be "stop wanting to do that".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the data from a void* in a struct
    By gwrodriguez in forum C++ Programming
    Replies: 8
    Last Post: 05-31-2011, 06:10 PM
  2. Array of structs -> struct to void
    By v1n1c1u5 in forum C Programming
    Replies: 1
    Last Post: 12-14-2009, 07:23 AM
  3. changing internal representation of a struct library
    By fisheromen1031 in forum C Programming
    Replies: 4
    Last Post: 11-13-2009, 06:24 PM
  4. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  5. '=' : cannot convert from 'void *' to 'struct HBRUSH__ *'
    By Bajanine in forum Windows Programming
    Replies: 9
    Last Post: 10-14-2002, 07:54 PM