In this lesson, we want to discuss the C pointer to answer some questions. Have you ever heard pointers? What is a pointer? Working with pointers for someone who codes in C language is obligatory.
A couple of lessons ago, we discussed variables in C language. Data is stored in memory, and each memory cell has a specific address. When a variable is defined to store specific data, the name of that variable is an alias for the variable address. Knowing the variable’s name makes it possible to store or retrieve data without dealing with the memory address.
Assume that the value 10 is stored in a variable named “a” whose address is 80F (the hexadecimal number) of the memory. “a” is an alias for the cell 80F of memory. What the programmer works with is “a” and not 80F. For a better understanding, pay attention to the following example:
int height = 8;
In this example, a memory location with a length of 2 bytes (because the int variable occupies two bytes of space) is intended to store the number 8. Its location is unknown and unimportant to us, but there is another type of data called “pointer,” which deals with a memory address.
Sometimes in programming, it is necessary to work with a variable address instead of the variable name. Pointers can hold the address of a variable (which is a numeric value). Like other data types that need to be defined first, the pointer must also be defined. The pointer definition is as follows:
<pointer type> *<pointer name>;
First, the pointer type must be specified. Pointer type can be char, int, float, or composite data. By specifying the pointer type, it is determined what type of variable the pointer points to. To understand the issue, pay attention to the following example:
char *pointer;
In this line, a variable named pointer has been declared, which is a pointer to a char variable.
To assign a value to a pointer, a char-type variable must be defined first. Pay attention to the following example:
char alphabet = 'A';
char *pointer;
pointer = &alphabet;
Look at the following image:
Note that the pointer type in C language and the variable it points to must be the same. Could you explain why this is so? If you wait, you will find the answer at the end of this lesson.
So far, it is explained how to assign a value(an address) to a pointer in C language. But how to access pointer content?
The asterisk (*) operator must be used to access the content of pointers. This operator extracts the content of the pointer (the content of the cell that the pointer points to). For a better understanding, consider the following example:
#include <stdio.h>
int main(){
int *x;
int z, y;
y = 142;
x = &y;
z = *x;
printf("%d %d %d\n", y, z, *x);
return 0;
}
The output is:
142 142 142
In this section, we want to review the types of operations allowed on pointers briefly:
It is possible to assign the value of one pointer to another pointer. They must be of the same type. Two pointers of different types cannot be assigned to each other unless we use type casting. For example, if ptr is a char pointer and p is an int pointer, then p = ptr is wrong and should be written as p = (int *)ptr, which is called type casting.
Type casting makes data of one type behave like data of another type. To understand this issue, pay attention to the following example:
float x;
x = 7/2;
float x;
x = (float)7/2;
It should be noted that the types of ordinary values are fundamentally different from pointers, and for this reason, assigning a non-pointer value to a pointer value is not correct.
The only allowed assignment of this type is zero assignment to a pointer. The C compiler guarantees that there is no location with zero memory in the program’s memory, and this assignment means that the pointer does not point to a specific location.
A pointer can be compared with zero to check if it is empty or not, and this operation is allowed. Instead of zero, the symbol constant NULL is used, which is available in the <stdio.h> header file. (header files are standard C language libraries that are available in all compilers and include widely used and standard C language functions and constants.)
It is also possible to compare two pointers, provided that both pointers point to the cells of an array. But if two pointers that point to the cells of two different arrays are compared, an unpredictable result will occur.
To learn about arrays, you can refer to the array lesson.
Addition (+), subtraction (-), increase by one unit (++), and decrease by one unit (–) can be performed on pointer variables. Because the pointer contains an address in memory, it shows a different behavior when calculations are performed on it.
Take the following example:
int *p, a;
p = &a;
p = p+4;
Now you can answer the above question. Why should the pointer and the variable it points to be similar?
The above example is used for the answer. If the pointer were of char type, the sum would point to 4 bytes ahead.
The increment-by-one (++) operator increments the value of a regular variable by one while moving the variable pointer to the number of bytes of its data type. For example, for a float data type pointer, since float has four bytes, 4 units are added to it when the pointer is incremented. So it points to the next 4 bytes of memory.