Home > Blockchain >  Is there an equivalent to ( (ptr i)->marks ), like ( ptr[i] .... )?
Is there an equivalent to ( (ptr i)->marks ), like ( ptr[i] .... )?

Time:09-17

I want to do something similar to the code below but using ptr[i]

printf("%s\t%d\n", (ptr   i)->subject, (ptr   i)->marks);

CodePudding user response:

ptr->x is the same as (*ptr).x.

*(ptr i) is the same as ptr[i].

Thus, by the transitive property, (ptr i)->x is the same as:

ptr[i].x

CodePudding user response:

Probably the question is how to dereference a pointer to a struct.

Some possible ways:

typedef struct
{
    int marks;
    char *subject;
}type;

void foo(type *ptr, ssize_t i)
{
    printf("%s\t%d\n", (ptr   i) -> subject, (ptr   i) -> marks);
    printf("%s\t%d\n", (*(ptr   i)).subject, (*(ptr   i)).marks);
    printf("%s\t%d\n", ptr[i].subject, ptr[i].marks);
    printf("%s\t%d\n", (&ptr[i]) -> subject, (&ptr[i]) -> marks);
}

CodePudding user response:

Yes.

Indexing with [i] is entirely equivalent to adding i and dereferencing the address thus produced. The indexing is simply "syntactic sugar", a convenient and customary short form. In fact, the indexing operation is defined in terms of adding an offset and dereferencing: p[i] is equivalent to *(p i).

One of the more unexpected consequences of defining indexing as addition plus dereferencing is that because addition is commutative (i.e., p i == i p holds for all p, i), against all appearances indexing is a symmetrical operator, too: for all p[i] i[p] is a valid, equivalent expression:

#include <stdio.h>

int main()
{
    // an array of anonymous structs with an int member
    struct { int mI; } arr[] = { {1}, {2}, {3} } ;
    
    // "sizeof arr/sizeof *arr": 
    // dividing the overall size of an array by the size of one element
    // is an idiomatic way to iterate through all elements without caring
    // about the exact count.
    for(int i=0; i < sizeof arr/sizeof *arr; i  )
    {
        // The printed expressions are all equivalent.
        printf( "%i is the same as "
                "%i is the same as "
                "%i is the same as "
                "%i is the same as "
                "%i is the same as "
                "%i\n", 
                arr[i].mI, 
                // Surprising: Indexing is commutative.
                // This may not pass code review, but it's perfectly legal C.
                i[arr].mI,  
                (*(arr   i)).mI,
                (arr   i)->mI,      // short form to access members of structures through a pointer
                (*(i   arr)).mI, // Less surprising: Addition is commutative
                (i   arr)->mI
        );
    }
}

Sample session:

$ gcc -Wall -o symmetrical symmetrical.c && ./symmetrical
1 is the same as 1 is the same as 1 is the same as 1 is the same as 1 is the same as 1
2 is the same as 2 is the same as 2 is the same as 2 is the same as 2 is the same as 2
3 is the same as 3 is the same as 3 is the same as 3 is the same as 3 is the same as 3
  • Related