What is a pointer?

Overview

Recently, while writing Golang for part-time jobs and hobbies, and writing C language for school lectures and assignments, I thought that pointers are difficult to understand, and when I talk about that story with various engineer students, it is difficult for everyone to understand. I thought, so this time I tried to summarize the pointers in C language (Golang was made with reference to C language)! !! !!

What is a pointer?

A computer reserves a variable in a program in memory and stores an address to identify its location. Pointers are used when the program wants to handle the address. In other words, a pointer is a variable that handles an address.

syntax

Let's look at the syntax for using pointers in C.

Declaration of pointer

`Data type * pointer variable name;`

(Example) Declare a pointer to an int type variable  int *p;

Substitute address

--You can get the address by adding "&" to the variable. &Variable name

(Example)


#include<stdio.h>
int main() {
    int x = 1;
    int *p;
    p = &x;
    printf("「&x "address: %p\n", &x);
    printf("Address indicated by "p": %p\n", p)

    return 0
}

(Execution result)


「&x "address: 00A2EE76
Address indicated by "p": 00A2EE76

Shows the value in the address indicated by the pointer

--If you add "*" to the pointer, you can get the value in the address indicated by the pointer.

*Pointer variable name;

(Example)

#include<stdio.h>

int main(){
    int x = 1;
    int *p = &x;
    printf("Value in the address indicated by "p": %d\n", *p);

    return 0;
}

(Execution result)


The value of the address indicated by "p": 1

Things to note

Attention 1

What I think is confusing and needs attention is how to interpret "". When assigning an address when declaring a pointer, it is "int * p = & x", so it looks as if "& x" is assigned to " p", but it is semantically "p = & x". It becomes. In other words, the following two are synonymous.

int x = 1;
int *p;
p = &x;
int x = 1;
int *p = &x;

Attention 2

The second point to note is about initialization. Just by declaring a pointer variable, the address indicated by that pointer variable becomes indefinite. Therefore, if you use the pointer variable without initializing it, you will access an unspecified address, which causes a bug.

#include<stdio.h>

int main() {
    int x = 1;
    int *p;
    //The following line causes a bug because p has not been initialized
    *p = x;
}

Arrays and pointers

You can work with arrays using pointer operations by assigning the start address of the array to a pointer variable. As you can see in the sample below, & num [0] and num are the same. Also, * (p + i) and num [i] are the same.

#include<stdio.h>

int main() {
    int *p;
    int i;
    int num[3] = {1, 5, 10};
    //The following line assigns the start address of the array to the pointer variable
    p = num;
    for(i = 0; i < 3; i++) {
        printf("%d\n", *(p + i));  //Advance the pointer by the number of elements to take the value
    }
    return 0;
}

(Execution result)

1
5
10

Summary

This time, I have summarized the basic points that are easy to misunderstand about pointers in C language. Next time, I'll summarize some of the misleading points about Golang pointers. !! !! !!

Recommended Posts

What is a pointer?
What is a distribution?
What is a hacker?
What is a decision tree?
What is a Context Switch?
What is a super user?
What is a system call
[Definition] What is a framework?
What is a callback function?
What is a python map?
[Python] What is a zip function?
[Python] What is a with statement?
What is a lexical scope / dynamic scope?
What is a Convolutional Neural Network?
What is namespace
What is copy.copy ()
What is Django? .. ..
What is dotenv?
What is POSIX?
What is Linux
What is SALOME?
What is python
What is hyperopt?
What is Linux
What is pyvenv
What is __call__
What is Linux
What is Python
What is a dog? Django installation volume
What is a dog? Python installation volume
What is a dog? Django--Create a custom user model
What is Piotroski's F-Score?
What is Raspberry Pi?
[Python] What is Pipeline ...
What is Calmar Ratio?
What is hyperparameter tuning?
What is Linux for?
What is ensemble learning?
What is TCP / IP?
It's a Mac. What is the Linux command Linux?
What is a dog? Django--Create a custom user model 2
What is Python's __init__.py?
What is an iterator?
What is UNIT-V Linux?
[Python] What is virtualenv
Tell me what a conformal map is, Python!
What is machine learning?
What is a dog? POST Sending Volume Using Django--forms.py
Basics of Python learning ~ What is a string literal? ~
What is a recommend engine? Summary of the types
What is God? Make a simple chatbot with python
To myself as a Django beginner (2) --What is MTV?
What is Minisum or Minimax?
What is Linux? [Command list]
What is Logistic Regression Analysis?
What is the Linux kernel?
Is this string a decimal?
Is this a system trade?
Python list is not a list
What is Google Cloud Dataflow?
[DL] What is weight decay?