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 ;<? }
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 ;<? }
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
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;
^
I recommend that you work through an introductory book on C.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am that where i got the example from
any help?
Then you should go through the chapter on pointers again, as well as the chapter on structs.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.Originally Posted by amahmoo
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:
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.Code:t1 x;- "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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by amahmoo
No. You have to understand what is t1.Originally Posted by amahmoo
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.Originally Posted by amahmoo
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Code:#include <stdio.h>but i get a error makes a pointer from integer without cast?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; }
i am confused on how to type caste in this situation
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?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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
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.Originally Posted by amahmoo
Compile this program:Originally Posted by amahmoo
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 = 1; 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".Code:int main(void) { void *p; p = 0; return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)