Pointers are variables that hold a memory location. Pointers can reference any data type, even functions. We’ll also discuss the relationship of pointers with text strings and the more advanced concept of function array of pointers in c pdf. Pointer types are often used as parameters to function calls.
The following shows how to declare a function which uses a pointer as an argument. Since C passes function arguments by value, in order to allow a function to modify a value from the calling routine, a pointer to the value must be passed. Pointers to structures are also used as function arguments even when nothing in the struct will be modified in the function. This is done to avoid copying the complete contents of the structure onto the stack. More about pointers as function arguments later.
So far we’ve discussed how to declare pointers. The process of assigning values to pointers is next. Pointers can also be assigned to reference dynamically allocated memory. The following is an example showing one pointer being assigned to another and of a pointer being assigned a return value from a function. When returning a pointer from a function, do not return a pointer that points to a value that is local to the function or that is a pointer to a function argument.
Pointers to local variables become invalid when the function exits. In the above function, the value returned points to a static variable. Returning a pointer to dynamically allocated memory is also valid. There is one more way of dereferencing a pointer, which will be discussed in the following section. When dereferencing a pointer that points to an invalid memory location, an error often occurs which results in the program terminating. The error is often reported as a segmentation error. A common cause of this is failure to initialize a pointer before trying to dereference it.
C is known for giving you just enough rope to hang yourself, and pointer dereferencing is a prime example. You are quite free to write code that accesses memory outside that which you have explicitly requested from the system. And many times, that memory may appear as available to your program due to the vagaries of system memory allocation. However, even if 99 executions allow your program to run without fault, that 100th execution may be the time when your “memory pilfering” is caught by the system and the program fails. Be careful to ensure that your pointer offsets are within the bounds of allocated memory!
You can assign a value to a void pointer, but you must cast the variable to point to some specified type before you can dereference it. Up to now, we’ve carefully been avoiding discussing arrays in the context of pointers. A variable declared as an array of some type acts as a pointer to that type. When used by itself, it points to the first element of the array.
A pointer can be indexed like an array name. The first case often is seen to occur when an array is passed as an argument to a function. The function declares the parameter as a pointer, but the actual argument may be the name of an array. The second case often occurs when accessing dynamically allocated memory. Let’s look at examples of each. Pointers and array names can pretty much be used interchangeably.