answersLogoWhite

0

The main advantage of a pointer is that, unlike an ordinary variable which can only refer to one object in memory (or an array of objects), a pointer can refer to any object in memory and we can change which object it refers to unless it is declared constant (const). This is an extremely powerful feature as it allows us to pass objects to functions by address, rather than by value, thus enabling the pass by reference mechanism.

A pointer is a variable just like any other. When we instantiate a pointer, memory is set aside for it and we can store a value in that memory, take its address, perform arithmetic on the value, and so on. Where it differs from an ordinary variable is that the stored value represents a memory address. We call them pointers because they "point to" that memory address and thus a pointer can "refer to" the object stored at that address.

[Note that in C, pointers are also known as references. However this usage of the term should not be confused with the C++ usage of the term, where a reference is an alias rather than a variable in its own right].

A pointer's type determines how the object it refers to will be interpreted. Thus when using pointers to refer to other objects, it is important that the pointer's type matches the type of object we wish to refer to. That is, if we want to refer to a char, we use a char pointer (char*), but if we want to refer to an int, we use an int pointer (int*). In some cases we may wish to refer to a multi-byte variable (such as a long int) just as if it were really an array of individual bytes or words (such as char or short). For this we simply use a pointer of the appropriate type (such as char* or short*) and cast the address of the object to that same type. For instance:

long int x=42;

char* p = (char*) &x; // take the address of x, treat as char* rather than long int*

Once we have an address stored in memory (in a pointer variable), we can perform "pointer arithmetic" upon it. This feature is primarily used to navigate through an array of values and, just as we can use the array suffix operator to access the individual elements of an array, we can use the array suffix operator on a pointer. Using the previous example, we can refer to the 3rd char of x as follows:

char c = p[2];

Given that p refers to a char, if we increment (or decrement) p, we will advance to the next (or previous) char address. Thus the above can also be written:

char c = *(p + 2);

Given the choice, the array suffix operator is clearly the better option. However, when traversing an array, the ++ and -- operators can be more convenient:

long int x = 42;

char* p; = (char*) &x;

char c;

int i;

for (i=0; i<sizeof(long int); ++i) {

// ...

++p; // advance to the next char element of x

}

While pointer arithmetic is useful, the main purpose of pointers is to refer to whole objects and to pass those objects to functions by reference rather than by value. Consider the following:

void foo (int x) {

// ...

}

When we pass an actual argument to this function, the value of that argument is assigned to the formal argument named x. Thus x is merely a copy of the actual argument. This means the function can modify the value of x without affecting the value passed by the caller. If we want the function to pass the modified value back to the caller, we would normally use the return value:

int foo (int x) {

++x; // modify x

return x; // return modified value

}

This is the recommended method of changing a value via a function, as it allows us a bit more flexibility in terms of what we do with that value:

int y = 42;

foo (y); // upon return, y is still 42 (the return value is lost).

y = foo (y); // upon return, y will be 43

int z;

z = foo (y); // upon return, y is still 43, while z is 44

However, the pass by value mechanism does not work for arrays, and with good reason; an array name only refers to the start address of the array. The array type determines how that address is interpreted but the type cannot be used to determine how many elements are actually in the array; the size of an array is not part of its type.

When we want to pass an array to a function, there are two ways we can declare the function:

void foo (int[], unsigned);

void foo (int*, unsigned);

Both versions mean exactly the same thing and will produce exactly the same machine code. Note that the unsigned argument is used to allow us to explicitly pass the length of the array.

Note that second version declares a pointer type. This means the function accepts the address of an object rather than its value. The same applies to the first version. This should come as no surprise given we've already seen how we can use the array suffix operator on a pointer; an array will implicitly convert to a pointer at the slightest provocation.

When we pass the address of an object, the function can refer to that object just as if we'd passed the object itself into the function (rather than just its value). This is what we mean by pass by reference. This means we must take the address of the object we wish to pass:

int x = 42;

// ...

foo (&x, 1);

Note that we pass the size as 1 because an object of type int has a length of 1 x sizeof(int) and the compiler already knows that an int has a length of sizeof(int). The size relates to the number of elements of the given type, not the number of bytes (or chars).

When we pass an actual array, we do not need to take the address because the name of the array implicitly converts to a pointer:

int y[100];

// ...

foo (y, 100);

Although there's no need to, we can explicitly take the address if we really want to:

foo (&y, 100);

Alternatively, we could take the address of the first element:

foo (&y[0], 100);

All three methods are functionally equivalent and will generate the exact same code.

Give that we can take the address of the first element, we can also take the address of any element and thus pass a subarray:

foo (&y[50], 10);

Here we are passing a subarray of 10 elements beginning at address y+50. This means we could also write this call as follows:

foo (&(y+50), 10);

Having passed an object by reference, the function can operate upon that object directly. This is useful when passing large objects as it takes just one CPU operation to assign an address to a pointer. Copying values larger than the size of a pointer is an expensive operation, so we only do that (explicitly) when we actually need a copy.

While arrays explicitly convert to a pointer, user-defined types do not. Thus when passing a struct or union object by reference, we must explicitly take the address of that object, just as we would with any other non-array type:

typedef struct st { /* ... */ };

void foo (struct s*);

int main (void) {

struct st s;

// ...

foo (&s);

// ...

return 0;

}

User Avatar

Wiki User

9y ago

What else can I help you with?

Related Questions

How accessing a pointer an a variable differs?

Pointer-variables are variables, so there is no difference.


Explain reference variable and how it is different from pointer variable?

In JAVA, all variables are reference variables, and there are no pointer variables. Even though the platform may implement them as pointers, they are not available as such. In C, no variables are reference variables. They are a C++ enhancement. In C++ a reference variable is syntactically the same as a pointer variable, except that the use of the indirection operator (*) is implicit. You do declare reference variables slightly differently than pointer variables but, once you do so, they can be treated as non-pointer variables. Reference variables also cannot be redefined once they have been initialized to point to some object. They are const. Structurally, there is no difference between a pointer variable and a reference variable. They are both still pointers. The compiler just makes it easier to treat reference variables and non-pointer variables the same way.


What is pointer?

pointer r the variables created in RAM which store the address of a another variable


Can a pointer be considered a variable?

You can declare pointer-variables, if that's what you mean. Example: char *sample = "Sample";


How many pointers can be used in a c program?

Answergenerally we use simple pointer, void pointer,null pointer, structure pointer. Answerzero or more (unlimited).


WHAT IS POINTER TO POINTER IN C POINTER?

Pointer in C is Memory Reference. It stores memory address of any variable, constant, function or something you later use in your programming. Pointer basically used to ease the referencing of variables and others or in polymorphism and inheritance.


How we can get address of variables in Java as pointer variables in C?

At any given point of time you cann't get the address of a variables of java program. This is meant for security purpose only.


Is declaration of variables allocated memory?

Constants, static variables and global variables are allocated in the program's data segment at compile time. Local variables are allocated on the stack at runtime. Variables cannot be allocated on the heap, you must use a constant, static variable, global variable or local variable to store the start address of a dynamic memory allocation. The variable must be a raw pointer or a reference handle (a smart pointer).


What is a pointer variable in C?

Pointer variables point to data variables. They are mostly used to point to dynamically allocated data variables, but can actually point to anything (e.g. statically allocated variables, array elements, anywhere inside a variable, program machine code, I/O device descriptors, nonexistent memory). Misuse of pointer variables, either unintentionally or intentionally, is a major cause of nearly impossible to debug software problems in programs written in C (and C++).


Different types of pointers in c language?

... are usable. void pointer (generic pointer) : a special type of pointer which point to some data of no specific types. void *p; null pointer : a special type of pointer which point nowhere. it is usually used to check if a pointer is pointing to a null or free the pointer during deallocation of memory in dynamic memory allocation; it is define by using the predefine constant NULL int *p=NULL; wild pointer : uninitialized pointer. it hold a garbage value. i.e it is not pointing to any memory location yet. dangling pointer: pointer pointing to a destroyed variable. it usually happen during dynamic memory allocation when the object is destroyed but not free and the pointer is still pointing to the destroy object.


What is this keywords?

'This' is a keyword that is it is previously defined in the library. It is Used with this pointer to point to another variables memory address.


Do you have pointer concept in c plus plus language?

Yes. All string variables are pointers as are other arrays.