Accessing inside structure via a struct pointer to a struct pointer
"
#include <stdio.h>
struct datastructure
{
char character;
};
void function(struct datastructure** ptr);
int main()
{
struct datastructure trial;
struct datastructure *structPtr;
structPtr=&trial;
structPtr->character='a';
function(&structPtr);
}
void function(struct datastructure** ptr)
{
*ptr->character='a';
printf("Ptr: %c",*ptr->character);
}
"
These codes give these errors:
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
these errors are related to
"
*ptr->character='a';
printf("Ptr: %c",*ptr->character);
"
I want to access "character" data inside the structure "trial" by a pointer to pointer "ptr" inside function "function",but I couldn't find a way to do this.
Re: Accessing inside structure via a struct pointer to a struct pointer
You should have written:
Code:
(*ptr)->character = 'a';
Otherwise, the compiler interprets it as *(ptr->character), but of course a pointer does not have a character member.
Re: Accessing inside structure via a struct pointer to a struct pointer
Thank you very much,really I was working on it for hours to make it work!
Re: Accessing inside structure via a struct pointer to a struct pointer
You're welcome :)
That said, why do you need to pass a pointer to a pointer? You are not modifying the pointer to the struct object, so just passing a pointer to the struct object will do.