Pointers in the C language

P

A pointer is a variable that holds a memory address. In C, a pointer can represent:

1. The address of a given type
– elementary type, structure, string, etc.
– pointer operations are determined by the size of the type of data.
2. The address of a function
– address where the current execution point will jump, if that function is called
3. The address of a memory address
– this type of pointer can be reduced to the first situation
4. Address of an area with unknown content (pointer to void)

The size of a pointer depends on the architecture and operating system on which the program was compiled. The size of a pointer is determined with sizeof (void *) and is not necessarily equal to the size of an entire data type. Declaring a pointer does not mean assigning a memory area in which data can be stored. A pointer is also a data type whose value is a number representing a memory address. For the de-referencing to take place successfully, the pointer must indicate the valid memory address that the program has access to. This address can be the address of a pre-declared variable or the address of a dynamically allocated memory block (as we will see later). Initializing pointers with the NULL constant, compatible with any type of pointer, indicating by convention an uninitialized pointer. A void pointer can not be used directly in pointer operations but must be converted to a pointer to a data type.

Types of pointers:
1. Pointers to date
2. Pointers to the paintings

A vector variable contains the start address of the vector and is therefore equivalent to a pointer to the vector element type. This equivalence is usually exploited in vector-type arguments and in working with dynamically allocated vectors.

The difference between a pointer variable and a vector name is that a vector name is a constant pointer (its address is assigned by compiler C and can no longer be modified at execution), so it can not appear to the left of an assignment, in while a pointer variable has modifiable content by attribution or arithmetic operations.

Also, a vector-type variable also contains information about the length of the vector and the total size occupied in the memory, while a pointer only describes a position in memory (it is a point value). The size of (v) operator for a vector in [N] of type T will be N * size of (T), while size of (v) for a variable of type T * will be size of (T *), pointer.

As a last note, it is important to note that a function can result in a pointer, but it can not result in a vector.

3. Pointers in positions
Within the functions, pointers can be used, among others, to:
– Transmitting results through arguments
– Sending an address through the result of the function
– Using different names (given by their addresses)
A function that needs to modify several values ​​received by arguments or that must deliver more results calculated within the function must use pointer arguments. A function that will modify two or more arguments will specify them individually by either a pointer or in a unified way, through a vector.
A pointer can not result in a pointer, but this pointer should not contain the address of a local variable. Usually, the pointer result is equal to one of the arguments, possibly modified in function.
A local variable has a temporary existence guaranteed only during the execution of the function in which it is defined (with the exception of local static variables), and therefore the address of such variable must not be transmitted out of function for later use.

Recent Posts

Archives

Categories