7

I've read that if we have a pointer to a struct type object we can't access the object field by using the asterisk (*) but we need to use this -> (like an arrow sign).

Now although I read about it I didn't understand why we can't use the asterisk, I'll try to provide a code example and a picture of how I visualize a struct in memory to help you understand what is preventing me from understanding this topic.

Okay, so here's our code :

#include <stdio.h>

typedef struct MyStruct
{
    char c1;
    char c2;
} MyStruct;

int main()
{
    MyStruct s1; // Let's assume s1's address is 0x34A
    s1.c1 = 'A';
    s1.c2 = 'B';
    printf("s1 c1 is %c and c2 is %c.\n", s1.c1, s1.c2);

    MyStruct *p = &s1; // Declaring our pointer to point to s1 address, so p points to 0x34A
    printf("s1 c1 is %c and c2 is %c.\n", p->c1, p->c2); // I know this is the valid way
    printf("s1 c1 is %c.\n", *p.c1); // I don't understand why this would be invalid
    return 0;
}

Now let me try to explain why I don't understand why this syntax is invalid. So p is pointing to the address of s1 which is 0x34A and as I see it this is how I see s1 in memory:

enter image description here

So what I imagine it as in memory is that s1 has it's own address of course which in our example is 0x34A and it points to c1 address in memory which is followed up by c2 address. So when I do this :

printf("s1 c1 is %c.\n", (*p.c1)++, *p.c2);

I think of it as I'm accessing the value that is inside the pointer address by our pointer so we're accessing the value which is c1.

But as I said it's invalid and I just breaking my head trying to understand why, I've tried to read about it but couldn't really put the finger on the answer that will satisfy me on why really, so I've provided the code example and the picture so you could get into my head and try and see why I'm thinking wrong here.

3
  • 1
    Because *p.c1 means that you want to dereference c1 (which is not a pointer). (*p).c1 would work, on the other hand, but you can see how it's finger-bending to pull that off (compared to simply p->c1). Commented Sep 9, 2014 at 15:53
  • @Groo so basically (*p).c1 is equivalent to p->c1 but just a different syntax ? no other differences ? like when u can do in a pointer to array for example *(p + i) and p[i] so just a different syntax ? Commented Sep 9, 2014 at 16:27
  • Yes, these expressions are equivalent. Commented Sep 9, 2014 at 21:41

2 Answers 2

8

*p.c1 is parsed as *(p.c1) not (*p).c1.

So when you do *(p.c1), you attempt to use the dot operator on a pointer, which fails.

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

4 Comments

so (*p).c1 is equivalent to p->c1 ?
To add some background why: the "."-Operator has higher precedence in C than the "*"-Operator. cf : en.cppreference.com/w/c/language/operator_precedence
@Akes55 which one is better practice to use?
@johngonidelis: Use p->c1, it's what everyone will expect.
0

There is a precedence problem over here. If you want to access like this then go for (*p).c1. Now by doing (*p), you will reach to your structure and then after members.

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.