Use pointer on struct

Asked

Viewed 174 times

0

I wanted to know how to access a certain element of a struct the way I’m defining it. Here’s my struct:

typedef struct {

 char *produtos[200000];
 int contador;

} Produtos,*ProdutosP;

Defini ProdutosP p as a pointer for struct. Agr accesses the first string of char *produtos[200000] using p->produtos[0] but I wanted to access the first character of the first string. How do I get what I want?

2 answers

1

You have defined products as an array of pointers to strings. To access the first character you only need to use one more [] In this case p->produtos[0][0]. The first number between the parentheses is the string and the second the character.

That is, the number between parentheses dereference the string at the position n and the second character in the chosen position.

The link I added is in English if you prefer has one in Spanish on wikipedia.

1


Agr access the first string of char *produtos[200000] using p->produtos[0] but I wanted to access the first character of the first string.

p->produtos[0][0]

or

*(p->produtos[0])

Browser other questions tagged

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