C++将Stutt属性设置为自己的地址

C++将Stutt属性设置为自己的地址,c++,struct,C++,Struct,我对C/C++有些陌生,所以假设我有一个简单的结构: struct item { int* address; std::string name; } int*是保存内存地址的正确数据类型吗? 我能不能用一下: item i; i.address = &i.; int j = 4; // create an int variable int *pToJ = &j; // create a pointer that contains the address o

我对C/C++有些陌生,所以假设我有一个简单的结构:

struct item {
   int* address;
   std::string name;
}
int*是保存内存地址的正确数据类型吗? 我能不能用一下:

item i; i.address = &i.;
int j = 4;        // create an int variable
int *pToJ = &j;   // create a pointer that contains the address of the int j.
要将structs属性设置为它自己的地址


通常,如果您只需要一个内存地址,那么可以将其存储在一个void*中,因为该类型没有任何指向某个对象的指针的含义

如果您想保留item::address是一个包含指向item对象的指针的成员的含义,那么它应该具有item*类型,如果合适的话,可以使用const进行适当的限定


当然,在C语言中,类型将改为struct item*,因为它将保存指向struct item对象的指针。通常,如果您只需要一个内存地址,则将其存储在void*中,因为该类型没有指向某个对象的指针的含义

如果您想保留item::address是一个包含指向item对象的指针的成员的含义,那么它应该具有item*类型,如果合适的话,可以使用const进行适当的限定

当然,在C中,类型将改为struct item*,因为它将保存指向struct item对象的指针

在C/C++中,指针是类型化的。您可以在任何指针中存储任何其他指针,因为它是内存中字节的唯一索引,但您不能自由转换它们,需要使用强制转换。通常,T类型变量上的指针应具有T*类型。因此,在您的示例中,应该是:

struct item {
   std::string * address;
   std::string name;
}
是的,你可以:

 item i;
 i.address = &(i.name);
这是如果您要存储成员名称的地址

如果要存储结构的地址:

struct item {
   item * address;
   std::string name;
}

item i;
i.address = &i;
在C/C++中,指针是类型化的。您可以在任何指针中存储任何其他指针,因为它是内存中字节的唯一索引,但您不能自由转换它们,需要使用强制转换。通常,T类型变量上的指针应具有T*类型。因此,在您的示例中,应该是:

struct item {
   std::string * address;
   std::string name;
}
是的,你可以:

 item i;
 i.address = &(i.name);
这是如果您要存储成员名称的地址

如果要存储结构的地址:

struct item {
   item * address;
   std::string name;
}

item i;
i.address = &i;
int*x;用于创建指向int的指针,以便执行以下操作:

item i; i.address = &i.;
int j = 4;        // create an int variable
int *pToJ = &j;   // create a pointer that contains the address of the int j.
对于结构,可以通过定义结构、创建结构变量、创建指向结构的指针,然后将结构变量的地址分配给结构指针变量来执行类似的操作。这与C类似:

struct __tagStruct {  // start a struct definition declaring a name of __tagStruct
  int  i;
  int  j;
};

struct __tagStruct myStruct;
struct __tagStruct *pMyStruct = &myStruct;

// or C++ allows you to use the struct name as a type name like a class
__tagStruct  myStruct2;
__tagStruct *pMyStruct2 = myStruct2;
如果希望结构包含自己的地址,则需要在结构定义中包含一个成员变量,该变量是指向结构类型的指针。这有点困难,因为为了定义指向结构的指针,必须首先定义或声明结构。幸运的是,对于指向结构的指针来说,仅仅声明结构定义存在就足够了。因此,您可以执行以下操作:

struct __tagStruct {      // starts the struct definition declaring a name of __tagStruct
   __tagStruct  *pMe;   // define a pointer variable to this struct type C++ style
   int    i;
   int    j;
};

struct __tagStruct  myStruct;    // define the struct variable C style
myStruct.pMe = &myStruct;

// or C++ allows you to use the struct name like a class for a type
__tagStruct  myStruct;
myStruct.pMe = &myStruct;        // assign to the pMe member the address of the struct
struct __tagStruct1;

struct __tagStruct2 {
  __tagStruct1 *pIt;    // pointer to the previously declared struct
  int  j;
};

__tagStruct1  myStruct1;
__tagStruct2  myStruct2;
myStruct2.pIt = &myStruct1;
如果结构包含指向不同结构类型的指针,则可以执行以下操作:

struct __tagStruct {      // starts the struct definition declaring a name of __tagStruct
   __tagStruct  *pMe;   // define a pointer variable to this struct type C++ style
   int    i;
   int    j;
};

struct __tagStruct  myStruct;    // define the struct variable C style
myStruct.pMe = &myStruct;

// or C++ allows you to use the struct name like a class for a type
__tagStruct  myStruct;
myStruct.pMe = &myStruct;        // assign to the pMe member the address of the struct
struct __tagStruct1;

struct __tagStruct2 {
  __tagStruct1 *pIt;    // pointer to the previously declared struct
  int  j;
};

__tagStruct1  myStruct1;
__tagStruct2  myStruct2;
myStruct2.pIt = &myStruct1;
int*x;用于创建指向int的指针,以便执行以下操作:

item i; i.address = &i.;
int j = 4;        // create an int variable
int *pToJ = &j;   // create a pointer that contains the address of the int j.
对于结构,可以通过定义结构、创建结构变量、创建指向结构的指针,然后将结构变量的地址分配给结构指针变量来执行类似的操作。这与C类似:

struct __tagStruct {  // start a struct definition declaring a name of __tagStruct
  int  i;
  int  j;
};

struct __tagStruct myStruct;
struct __tagStruct *pMyStruct = &myStruct;

// or C++ allows you to use the struct name as a type name like a class
__tagStruct  myStruct2;
__tagStruct *pMyStruct2 = myStruct2;
如果希望结构包含自己的地址,则需要在结构定义中包含一个成员变量,该变量是指向结构类型的指针。这有点困难,因为为了定义指向结构的指针,必须首先定义或声明结构。幸运的是,对于指向结构的指针来说,仅仅声明结构定义存在就足够了。因此,您可以执行以下操作:

struct __tagStruct {      // starts the struct definition declaring a name of __tagStruct
   __tagStruct  *pMe;   // define a pointer variable to this struct type C++ style
   int    i;
   int    j;
};

struct __tagStruct  myStruct;    // define the struct variable C style
myStruct.pMe = &myStruct;

// or C++ allows you to use the struct name like a class for a type
__tagStruct  myStruct;
myStruct.pMe = &myStruct;        // assign to the pMe member the address of the struct
struct __tagStruct1;

struct __tagStruct2 {
  __tagStruct1 *pIt;    // pointer to the previously declared struct
  int  j;
};

__tagStruct1  myStruct1;
__tagStruct2  myStruct2;
myStruct2.pIt = &myStruct1;
如果结构包含指向不同结构类型的指针,则可以执行以下操作:

struct __tagStruct {      // starts the struct definition declaring a name of __tagStruct
   __tagStruct  *pMe;   // define a pointer variable to this struct type C++ style
   int    i;
   int    j;
};

struct __tagStruct  myStruct;    // define the struct variable C style
myStruct.pMe = &myStruct;

// or C++ allows you to use the struct name like a class for a type
__tagStruct  myStruct;
myStruct.pMe = &myStruct;        // assign to the pMe member the address of the struct
struct __tagStruct1;

struct __tagStruct2 {
  __tagStruct1 *pIt;    // pointer to the previously declared struct
  int  j;
};

__tagStruct1  myStruct1;
__tagStruct2  myStruct2;
myStruct2.pIt = &myStruct1;

我假设地址指针的类型应该足够大,可以容纳16、32或64位的地址。此外,是项目*地址;与项目*地址相同@C/C++中的James_Parsons所有指针的大小都相同。在32位平台上,它们是32位的,在64位平台上,它们是64位的。@James:从技术上讲,地址在C/C++中是不透明的:标准中没有任何指针具有数值的概念。当然,指针在汇编中通常是以这种方式实现的,但也有其他可能。@James_Parsons是的,空格在这里不起任何作用:item*address;==项目*地址@Hurkyl:不,C++和C指针不是不透明的。它们支持许多被称为指针算术的算术运算,也可以与足够大的整数进行转换。也许您正在考虑引用或其他语言。我假设地址指针的类型应该足够大,可以容纳16、32或64位的地址。此外,是项目*地址;与项目*地址相同@C/C++中的James_Parsons所有指针的大小都相同。在32位平台上,它们是32位的;在64位平台上,它们是64位的。@Jam
es:从技术上讲,地址在C/C++中是不透明的:标准中没有任何指针具有数值的概念。当然,指针在汇编中通常是以这种方式实现的,但也有其他可能。@James_Parsons是的,空格在这里不起任何作用:item*address;==项目*地址@Hurkyl:不,C++和C指针不是不透明的。它们支持许多被称为指针算术的算术运算,也可以与足够大的整数进行转换。也许你在想参考资料,或者其他语言。