C pointer – What is a pointer?


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.

Variables Overview

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.

What is a C pointer?

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.

Assigning a value to a pointer

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;
  1. On line one, a character type variable named alphabet is defined, and the ASCII code of character A is stored in it.
  2. On line two, a variable named pointer with type char is defined.
  3. On line three, the address of the variable alphabet is extracted by the address operator (&) and stored in the pointer.

Look at the following image:

c pointer

 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.

Pointer access

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;
}
  1. A pointer of type int is defined on line 4.
  2. On line 5, variables z and y are defined as int type.
  3. On line 6, the variable y is set with the number 142.
  4. On line 7, the address of the variable y is placed inside the pointer x.
  5. On line 8, the content of the memory cell pointed to by the pointer x (x points to the address of the variable y) is placed into the variable z, and z has the value 142.

The output is:

142 142 142

Operations on pointers

In this section, we want to review the types of operations allowed on pointers briefly:

Allocation

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;
  1. At first, a variable x of type float is defined.
  2. Then, the result of dividing 7 by 2 is assigned to x. Will the value of x be decimal?
  3. No, the value of x is an integer because 7 and 2 are integer types. So what is the solution? We must use casting as follows:
float x;
x = (float)7/2;
  • In this case, the presence of float causes casting, and the decimal value is placed in x.

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.

Comparison

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.

Increase and decrease by one number

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;
  1. On line one, variable a and pointer p are defined.
  2. On line two, the address of the variable a is placed inside the pointer p.
  3. On the third line, the p pointer is added to 4.
  4. P is of type int and has two bytes length. So, when it is added to 4, it goes forward by 2×4, i.e., 8 bytes.
  5. The int pointer holds the address of two-byte variables. When added to 4, it means to point to 4 addresses ahead, and each address occupies 2 bytes. So the sum with 4 points to 8 bytes ahead.
  6. If the address of variable a is equal to 1000. The result of this sum is equal to 1008.

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.

Was this helpful?
[0]
Scroll to Top
Scroll to Top