Pointer arithmetic in C

Asked

Viewed 107 times

2

I am doubtful in a way to know the length of a vector without using sizeof:

int n = *(&arr + 1) - arr;

It is known that:

arr is the type int ( * ) and (&arr + 1) is the type int ( * )[size].

What is the function of *?

It’s to get the address amount (&arr + 1) or to cast a int ( * )[size] for int ( * )?

1 answer

3


The operator * (asterisk or star) is always used to "derreference" an address, ie, it takes the value that is in that address, nothing more than this, has nothing to do with cast. To tell you the truth I don’t even know what the rest of the question is about.

There the operator & returns a reference (arr is already a reference), you have the reference reference, when you do the dereference is only with a reference.

  • Sorry if you got confused, I’ll try to elaborate better.

  • when I print (&arr + 1) a reference in memory is returned which is equal when I print *(&arr + 1). The first doubt is why *(&arr + 1) returns a reference in memory if I’m using the operator to drift?

  • Why the operator & returns a reference (arr is already a reference), you have the reference reference, when you do the dereference is only with a reference.

  • Thank you very much. I had not attempted it. Now it became clearer.

Browser other questions tagged

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