Pointer Use

Why do we use pointer instead of variable?

Questions by nipa123456

Showing Answers 1 - 9 of 9 Answers

We use pointers to directly access the variable's address, the concept is more clear when we talk about function callings, when we use the call by reference method, we change the original value (the value at the address) and there we use the pointers and when we talk about call by value method, we first make the copy of the original value and do changes to the copy and hence no need of using the pointer.

  Was this answer useful?  Yes

There is one more difference between variables and pointers. Suppose we want to modify the value of a variable directly then first of all compiler will fetch the value by going into the address of the variable but this is not the case with pointers as in case of pointers compiler directly fetch the value of variable.

  Was this answer useful?  Yes

Cmkraj

  • Sep 15th, 2010
 

List of pros & cons of pointers over variables
1. Fetching speed is faster thru pointers as pointers directly access the memory
2. Useful in the concepts of pass by reference. variable value can be changed by passing them as pointer parameters instead of variables.
3.
You can perform pointer arithmetic's
4.  Pointer variables can be reused & pointers to pointers possible
5. Special case - char
variable can hold a single character. But a pointer can hold many
char *a="Hello"; // pointer pointing to a string
6.
Pointers are as safe as they prone to errors. Careful handling of pointers have
tremendous performance to the code

  Was this answer useful?  Yes

We *must* use pointers for the following:

1. To fake pass-by-reference semantics;
2. To track dynamically allocated memory;
3. To build self-referential data structures.

If we want a function to modify one of its parameters, we must pass a pointer to that parameter:

void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}

In the function above, we are not writing to a or b, but to what a and b point to.

The malloc(), calloc(), and realloc() functions all return pointer values (specifically, pointers to void); in order to work with dynamic memory, we must use pointer types.

If we want to build a data structure that can refer to another instance of itself, we must use a pointer type:

struct tree {
data_t data;
struct tree *left;
struct tree *right;
};

We have to do this because the struct type definition is not yet complete before left and right have to be defined, and because if the compiler allowed left and right to be instances of struct tree instead of pointers, it would have to allocate an infinite amount of space for each instance (one instance of struct tree would contain two instances of struct tree, each of which would contain two instances of struct tree, each of which would contain two instances of struct tree, etc.).

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions