简体   繁体   English

指向结构内部变量的指针

[英]Pointer to variable inside a struct

I have a struct like this: 我有一个这样的结构:

typedef struct{
    char *lexema;
    int comp_lexico;
    union{
        double v;
        double (*fnctptr)();
    } valor;
}tipoelem;

struct celda {
    tipoelem info;
    struct celda *izq, *der;
};

typedef struct celda * abb;

Then I define a global variable abb , which has a global scope. 然后,我定义一个具有全局范围的全局变量abb If I get somehow the memory direction of the field info of celda , would I be able to modify it safely or It is better to define the field as a tipoelem pointer such as (tipoelem *info) ? 如果我以某种方式celda字段信息的存储方向,是否可以安全地对其进行修改?还是最好将字段定义为tipoelem指针,例如(tipoelem *info)

The thing is, is it safe to edit the tipoelem info field with a tipoelem *pointerToInfo from other part of the prrogram or it is better to declare it as a pointer tipoelem *info in the struct celda? 事实是,使用tipoelem *pointerToInfo其他部分的tipoelem *pointerToInfo编辑tipoelem info字段是安全的还是最好在结构celda中将其声明为指针tipoelem *info

Edited with more information: The way I want to modify tipoelem info is the next one, and I do not know if it is safe. 编辑更多信息:下一个tipoelem info是我想要修改tipoelem info方式,我不知道它是否安全。

abb a;
int main(){
    tipoelem *ptr = a->info;
    ptr->comp_lexico = 2;
}

I define a global variable abb, 我定义了一个全局变量abb,

It is not a variable, it is a type . 它不是变量,而是type


As an answer to the question after the edit:- 作为编辑后问题的答案:-

Now you can use abb , as a type similar to struct celda* . 现在您可以使用abb ,其类型类似于struct celda*

After the edit the answer is simply what the problem demands. 编辑后,答案就是问题所需要的。

For example: 例如:

if each instance of the struct celda is needed to contain a block information then it is better to allow a pointer to it inside the structure . 如果需要struct celda每个实例包含一个块信息,那么最好在structure内部允许一个指向它的指针。 That is a better thing in terms of design where closely related data are kept closely. 就紧密关联的数据保持紧密的设计而言,这是一件好事。

And another thing is suppose you keep a pointer to tipoelem outside of the structure variable. 另一件事是假设您在该结构变量之外保留了指向tipoelem的指针。 Then suppose after some time you will need another pointer to tipoelem then these two pointer to tipoelem is different in meaning. 然后假设一段时间后,您将需要另一个指向tipoelem指针,那么这两个指向tipoelem指针的tipoelem是不同的。 That's why it's better to keep in structure. 这就是为什么最好保持结构。

To access the int inside info with a variable of type abb I would use this main: 要使用abb类型的变量访问int内部信息,我将使用以下main:

#include <stdio.h>
void main(){
    abb variable;
    printf("comp_lexico es %d",variable->info.comp_lexico);
}

Yes, it is safe to access like that. 是的,这样访问是安全的。 Consider this example, 考虑这个例子,

#include<stdio.h>

struct stOne {
    int a;
    int b;
    char c;
};

struct stTwo {
    struct stOne ObjectOne;
    struct sttwo *pTwo;
};

struct stOne *pOne;
struct stTwo *pTwo;
struct stTwo ObjectstTwo;

int main() {

    pTwo = &ObjectstTwo;   
    pTwo->ObjectOne.c = 'H';
    printf("%c", ObjectstTwo.ObjectOne.c);

    pOne = &pTwo->ObjectOne;
    pOne->c = 'D';
    printf(" %c", ObjectstTwo.ObjectOne.c);
}

This code prints 此代码打印

H

as output. 作为输出。

Similarly you can modify a member of the struct tipoelem , for example comp_lexico which is an int . 类似地,您可以修改struct tipoelem的成员,例如comp_lexico ,它是一个int

struct celda objectCelda;

//Assuming abb is a pointer, Make abb point to an object
abb = &objectCelda;

//Modify the value of comp_lexico
abb->info.comp_lexico = 0xAA;

Now changing the code, 现在更改代码,

#include<stdio.h>

struct stOne {
    int a;
    int b;
    char c;
};

struct stTwo {
  struct stOne ObjectOne;
  struct sttwo *pTwo;
};

struct stOne *pOne;
struct stTwo *pTwo;
struct stTwo ObjectstTwo;

int main() {

    pTwo = &ObjectstTwo;   
    pTwo->ObjectOne.c = 'H';
    printf("%c", ObjectstTwo.ObjectOne.c);

    pOne = &pTwo->ObjectOne;
    pOne->c = 'D';
    printf(" %c", ObjectstTwo.ObjectOne.c);
}

This prints 此打印

H D

on console as output. 在控制台上作为输出。 So it is perfectly okay to do it either way. 因此,无论哪种方式都可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM