2

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

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?

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.

abb a;
int main(){
    tipoelem *ptr = a->info;
    ptr->comp_lexico = 2;
}
3
  • 3
    could you reformulate, the question is hard to understand; what quind of code would you like to write, and where in this code do you think there is a problem? Commented Nov 15, 2017 at 9:57
  • Don't hide pointers by typedef. Commented Nov 15, 2017 at 10:05
  • @OznOg edited with more information Commented Nov 15, 2017 at 10:19

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

It is a bit different. What I want to do is edit object one having ptOne = &(pTwo->objectOne) and then ptOne->c = 'H'. I do not know if that is safe.
0

To access the int inside info with a variable of type abb I would use this main:

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

Comments

0

I define a global variable abb,

It is not a variable, it is a type.


As an answer to the question after the edit:-

Now you can use abb, as a type similar to 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. 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. Then suppose after some time you will need another pointer to tipoelem then these two pointer to tipoelem is different in meaning. That's why it's better to keep in structure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.