How to access a pointer within a structure?

Asked

Viewed 315 times

4

I need to know how to access the first position of the pointer vector *c_parte_real, as shown below:

typedef struct{

   struct char_vector{
      char *c_parte_real[2], *c_parte_imag[2];
   }c_vector;

   struct int_vector{
      int *i_parte_real[2], *i_parte_imag[2];
   }i_vector;

   struct complex_num{
      float real1,real2,imag1,imag2;
   }comp_num;

}expressao_complexa;

3 answers

2

Assuming you declare a variable x, of the kind expressao_complexa, access can be made directly:

...
expressao_complexa x;

// atribui um valor
x.c_vector.c_parte_real[0] = "1.0"; 

// imprime o valor atribuído
printf("%s\n", x.c_vector.c_parte_real[0]); 
...

1


I gave a simplified to be clearer, I created a variable to receive the position value

#include "stdio.h"

int main(void) {


typedef struct char_vector{
char *c_parte_real[2], *c_parte_imag[2];
}c_vector;



char  a,b;//variável que recebe o valor
c_vector x;

x.c_parte_real[0]='2';
a = x.c_parte_real[0];

x.c_parte_real[1]='3';
b = x.c_parte_real[1];

printf("a=%c\n",a);//imprime na tela
printf("b=%c\n",b);//imprime na tela

return 0;
}

test on :https://repl.it/languages/c

0

The above answers are correct, but your code is wrong, you cannot declare structures within structure statements. What you can do is have structures as components of structure statements.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.