Pointers
Each variable has its address in memory, a storage unit where it "lives" during the program's execution. To access the address, you can use ampersand &. For instance:
12int x; cout << &x;
It means that the address of the variable x for this execution of your code is 0x7ffd586e0344. With each execution, the address can change since the program can start at any part of your memory.
A pointer is a variable that stores the address of another variable. To declare the pointer use the asterisk *. For example, let's declare a pointer to the variable type of double:
double *pa;
Like with variables, we have to name the pointer and define the type it points to by declaration.
Let's learn how to assign the address of the variable to a pointer. For example, we want to know where the variable b is. Let's write down its address to the pointer pb:
int b = 42;
int *pb;
pb = &b;
To assign to the pointer the variable address, use the pointer's name without *. Or you can define the pointer by the declaration:
int b = 42;
int *pb = &b;
1. What is the pointer?
2. Output the address of the variable x.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Posez-moi des questions sur ce sujet
Résumer ce chapitre
Afficher des exemples du monde réel
Génial!
Completion taux amélioré à 2.94
Pointers
Glissez pour afficher le menu
Each variable has its address in memory, a storage unit where it "lives" during the program's execution. To access the address, you can use ampersand &. For instance:
12int x; cout << &x;
It means that the address of the variable x for this execution of your code is 0x7ffd586e0344. With each execution, the address can change since the program can start at any part of your memory.
A pointer is a variable that stores the address of another variable. To declare the pointer use the asterisk *. For example, let's declare a pointer to the variable type of double:
double *pa;
Like with variables, we have to name the pointer and define the type it points to by declaration.
Let's learn how to assign the address of the variable to a pointer. For example, we want to know where the variable b is. Let's write down its address to the pointer pb:
int b = 42;
int *pb;
pb = &b;
To assign to the pointer the variable address, use the pointer's name without *. Or you can define the pointer by the declaration:
int b = 42;
int *pb = &b;
1. What is the pointer?
2. Output the address of the variable x.
Merci pour vos commentaires !