[C] Pointer

3 minute read

Pointers can be understood as literally pointing to, pointing action objects. Regardless of the role of a pointer, since it is also an object, it can be defined and used as a variable. Introduced in the post about Data type, a variable that acts like an integer/real/character data type is a pointer variable, and the object it points to is a memory address. When expressing a pointer variable, the * operator is used, which is called the indirect operator. It should be noted that the usage form after declaration and initialization differs depending on the presence or absence of the * operator. Looking at the example below, it can be seen that the * operator was used when declaring a pointer variable, and the * operator was not used when the address value was directly input, but the * operator was used when outputting the value. When learning pointers for the first time, the two usages can be confusing, so it’s good to remember separately when to declare and initialize and when to use variables afterwards.

    #include <stdio.h>

    int main(){
        int *p;
        int a=1;

        p= &a;
        printf("%d %d", p, *p);

        return 0;
    }

6422036
1

When declaring a variable, the * operator is used to specify that it is a pointer variable. When a pointer variable receives an address, the * operator was not used, and the * operator was used to access the pointed address and check its value. In order for the pointer variable to store the address and move, edit, etc. based on this, the target data type is required. In the example, a pointer variable is also declared as int to receive an address for an integer data type. This pointer variable is moved in units of 4B, which is the unit of the int variable, and if it is a pointer variable for char, it is moved in units of 1B.

In order to understand why pointer variables are used in this way, it is necessary to know about Call by value / Call by address. Unlike passing a value to a in the example above, a pointer variable receives an memory address and accesses the value through the memory address. When calling an memory address is more advantageous than calling a value, a pointer variable is used, one of which is the use of variables in functions. If you understand the concept of global and local variables, you know that it is not possible to refer the values that are not received as input and that not possible to pass many values to a function at once, such as an array. In this part, the C language tried to increase the efficiency in operation time and method by passing only the address of a variable and an array using a pointer variable.

For arrays and pointer variables, it is necessary to understand the equivalence relationship in terms of addresses. Looking at the simple array declaration int a[10], there are 10 integer elements of the a array. At this time, a itself plays the same role as a pointer variable. That is, it plays the role of delivering an address. Accessing an already declared array such as a[4] means accessing the 5th element of a, so it can be understood that a itself serves as a criterion for the group variable. In fact, if only the name of the array is used, it can be used as an address, and the meaning at that time matches the address of the first element of the array. So, when passing an address to an input value in a function such as scanf, it passes &a[0], but it also passes a as it is.

 a== &a[0];
 *a = a[0];
 *(a+1)=a[1];
 *(a+2)=a[2];

 *(a+i)=a[i]
#include <stdio.h>

    void func(int *x){
        *(x+4)= 30;
        
    }


    int main(void){
       int x[5]={2,4,6,8,10};

       func(x); 
       printf("%d", x[4]);

        return 0;
    }


30

func(int *x) is a marker to receive an address value as an input, and it means that a pointer variable is defined and an input address is received in the form of this pointer variable. In other words, it receives an address as an input, stores this address in a pointer variable defined in the function, and uses it in the function. In this way, it is possible to access and modify the data written in the address by taking the input address value inside the function and going outside.

Pictures related to the pointer concept will be updated in the future.