Click here to Skip to main content
14,937,395 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Consider a structure with various variables

Is it possible that I edit the variable inside it without using their names (only addresses )?

so far I can't seem to do it. Even if I can export values using *pointer, writing something if the for of *pointer=value resutls to a compiler error

"Error	1	error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion"


I've tried type casting but it doesn't seem to work !! can I do it somehow ?
Posted
Updated 2-Dec-14 17:18pm
v2
Comments
   
Instead of "variable", you should have said "field". Of course you can do it, why not?
—SA
barneyman 2-Dec-14 23:16pm
   
you can, you have to cast, so, in the above case it would be something like *((int*)pointer)=value

but really, why? The compiler can do what it wants with a structure, packing wise, so trying to poke at it is never guaranteed to work - what problem are you trying to solve?
   
To cast this way, the structure should be compatible with int somehow, which is not the fact. This is nothing but OP misconception. Please see my answer.
—SA

Example code:
C++
struct MyStruct {
   char c;
   int i;
   double d;

   char* get_c() {
      return &c;
   }
   int* get_i() {
      return &i;
   }
   double* get_d() {
      return &d;
   }
};

void foo() {
   MyStruct s;
   char* pc = s.get_c();
   *pc = 'a';
   *(s.get_i()) = 1;
   double& rd = *(s.get_d());
   rd = 3.14;
}

This code uses a local pointer variable, a temporary, and a local reference to access members of the struct. All of these should work.

Note my use of accessor functions: you didn't state what you intended, but if you're accessing these member variables multiple times, it's usually a good idea to have a function for that purpose.

P.S.:
Member 10964099 wrote:
"Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion"

This error pretty clearly states what you did wrong: the right hand side obviously evaluates to an integer value, but the left hand side evaluates to a type that the compiler can't assign an integer to. The reason is that your pointer variable is not of the appropriate type - it must be pointer to int, or pointer to any other type that an int can be converted into, e. g. pointer to long or pointer to double.
   
v2
Yes, you can do it, however, most of the times it is not required (and not a wise thing to do :-) ).
Consider the following code:
C++
#include <iostream>
using namespace std;


struct Point
{
  int x, y;
};

void dump(const Point & p){ cout << p.x << ", " << p.y << endl; }

int main()
{
  Point p;
  p.x = 10;
  p.y = 20;

  dump(p);

  int * pi;

  pi = (int *) ((unsigned char *) &p + sizeof(int)); // skipping x, get y address
  *pi = 50;

  dump(p);

}

The output is:
10, 20
10, 50


The pi pointer accesses the y member of the Point struct without using member variable name (uses its computed address).
   
v2
Comments
Stefan_Lang 4-Dec-14 6:19am
   
This approach can be somewhat dangerous as the compiler is allowed to store data members in different order under certain conditions. See http://stackoverflow.com/questions/916600/can-a-c-compiler-re-order-elements-in-a-struct
CPallini 4-Dec-14 14:51pm
   
Of course, you have to know what you are doing.
First, you pointer should not be a const pointer, and it should be strictly typed, such as in MyStructure * pStr = new MyStructure(); and then use the member accessz '->' or '.' operator:
pStr->MyField = newValue; that's all. The field should be accessible where it is used; this is the matter of context and access declarations.

Your mistake was not mentioning the field name. How could you possibly assign a value to who-knows-what?

Please see:
http://www.cplusplus.com/doc/tutorial/operators[^],
http://www.tutorialspoint.com/cplusplus/cpp_class_access_modifiers.htm[^].

—SA
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900