Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How is this passing of pointers through function working in C?

This is a C code. The aim of the function is to add data which is defined using data pointers ‘*data’

int chksum(int *data) {
    char j;
    int summation = 0;

    for (j = 0; j < 64; j++) {
        summation += data[j]
    }

    return summation;
}

But I am not able to understand that how the data given by the pointer ‘*data’ is getting added using summation += data[j].

My understanding says, the code should be like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int chksum(int *data) {
    char j;
    int summation = 0;

    for (j = 0; j < 64; j++) {
        summation += *(data++)
    }

    return summation;
}

>Solution :

We could use *(data++).

But surely you see that’s the same as *(data+j).

And since data[j] is equivalent to *(data+j) by definition, we could also use data[j].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading