Hi Friends,
I have a small doubt about pointers,
One of the Advantage of the Pointers is "FAST ACCESS" comparitively to variables
My doubt is How can it be?
a Pointer stores a variables address, whenever it accessed it goes to that particular address and retrives the value...
Similarly,a variable stores the value,However Compiler goes to the particular address and retrives the value.This must be same in the both cases.then pointer must be of same speed as variable/less than it(I think).
Can u clarify my doubt

>One of the Advantage of the Pointers is "FAST ACCESS" comparitively to variables
I think you misunderstood whoever told you that. The performance difference of pointers is usually described in terms of array indexing:

int a[10] = {0,1,2,3,4,5,6,7,8,9};
int *p;
int i;

for (i = 0; i < 10; i++)
  a[i] *= 2; // Theoretically slower (more operations)

for (p = a; p != a + 10; i++)
  *p *= 2; // Theoretically faster (fewer operations)

a[i] needs to calculate the address offset from a base address (&a[0]) and then dereference that address, while *p is already at the correct offset and simply needs to dereference.

If you have a non-array variable and a pointer to that variable, the pointer obviously has more work to do because of the added level of indirection.

My guess is also that a pointer to a struct would be faster when passed as a function parameter, instead of copying the whole structure.

For example: When c code is translated into assembly;
Passing a whole struct as a parameter would force the program to store all values on the stack before calling the function, when the subroutine later is called it fetches the values using the stack pointer.

But, when passing the structure only as a pointer the subroutine can directly fetch the wanted values using the passed pointer, which probably is a 16-64 bit address.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.