c ++结构指针 [英] c++ structure pointers

查看:59
本文介绍了c ++结构指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个包含各种变量的结构



我是否可以在不使用其名称(仅地址)的情况下编辑其中的变量?



到目前为止,我似乎无法做到这一点。即使我可以使用*指针导出值,如果*指针=值的for重新编译为编译器错误,也要写一些内容



错误1错误C2679:二进制'=':找不到哪个操作符采用'int'类型的右操作数(或者没有可接受的转换





我尝试过类型转换,但它似乎不起作用!!我能以某种方式做到吗?

解决方案

示例代码:

  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 ;
}



此代码使用本地指针变量,临时和本地引用来访问结构的成员。所有这些都应该有效。



注意我对访问器函数的使用:你没有说明你的意图,但如果你多次访问这些成员变量,为这个目的设置一个函数通常是一个好主意。



PS:

会员10964099写道:

错误1错误C2679:二进制'=':找不到运算符,它采用'int'类型的右手操作数(或者没有可接受的转换



这个错误非常清楚地说明了你做错了什么:右侧显然评估为一个整数值,但左侧评估为编译器无法分配整数的类型。原因是你的指针变量是不是合适的类型 - 它必须是指向 int 的指针,或指向int可以转换成的任何其他类型的指针,例如指向 long的指针或指向 double 的指针。


是的,你可以这样做,但是,大部分时间都不需要(并且不是明智的事情:-))。

请考虑以下代码:

  #include   <   iostream  >  
使用 命名空间标准;


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 )); // 跳过x,获取y地址
* pi = 50 ;

dump(p);

}



输出结果为:

 10,20 
10,50





pi 指针访问 y Point struct 的成员,不使用成员变量名称(使用其计算的地址) )。


首先,你的指针不应该是一个const指针,它应该是严格类型的,例如在 MyStructure * pStr = new MyStructure(); 然后使用成员accessz' - >'或'。'运算符:

pStr-> MyField = newValue; 就是这样。该字段应在可以使用的地方访问;这是上下文和访问声明的问题。



你的错误是没有提到字段名称。你怎么可能为谁知道什么分配价值?



请参阅:

http://www.cplusplus.com/doc/tutorial/operators [ ^ ],

http://www.tutorialspoint.com/cplusplus/cpp_class_access_modifiers.htm [ ^ ]。



-SA


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 ?

解决方案

Example code:

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.


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:

#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).


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


这篇关于c ++结构指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆