Pointers
Using the reference operator &
and the dereference operator *
, we can create and work with pointers.
A pointer is a data type, just like int
, char
, or double
.
The pointer is designed to store an address, which you can obtain using the &
operator.
To declare a pointer, prepend it with the *
character.
int* intPointer; // pointer to an int variable
double* doublePointer; // pointer to a double variable
char* charPointer; // pointer to a char variable
In essence, a pointer is a variable that holds the address of another object.
int x = 100; // variable
int* pX; // pointer to an int variable
pX = &x; // pX now points to x
Note
Typically, pointers are named by prefixing the letter
p
to the name of the object they're pointing to.
When you dereference a pointer, you access the value of the variable it points to.
Main.c
1234567891011121314#include <stdio.h> int main() { int x = 22543; // variable int* pX = &x; // `pX` is pointer to `x` printf("The value of `pX` is %p\n", pX); // value of pointer `pX` printf("The value of `x` by pointer `pX` is %d\n", *pX); // pointer dereference return 0; }
Note
*(&variable) == *pVariable
If you attempt to dereference a null pointer, the compiler will raise an error:
Example provided to showcase the mistake
Main.c
12345678910#include <stdio.h> int main() { int* pX; printf("x = %p", pX); return 0; }
Swipe to start coding
Determine the size of int
and double
pointers.
Oplossing
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 2.63Awesome!
Completion rate improved to 2.63
Pointers
Using the reference operator &
and the dereference operator *
, we can create and work with pointers.
A pointer is a data type, just like int
, char
, or double
.
The pointer is designed to store an address, which you can obtain using the &
operator.
To declare a pointer, prepend it with the *
character.
int* intPointer; // pointer to an int variable
double* doublePointer; // pointer to a double variable
char* charPointer; // pointer to a char variable
In essence, a pointer is a variable that holds the address of another object.
int x = 100; // variable
int* pX; // pointer to an int variable
pX = &x; // pX now points to x
Note
Typically, pointers are named by prefixing the letter
p
to the name of the object they're pointing to.
When you dereference a pointer, you access the value of the variable it points to.
Main.c
1234567891011121314#include <stdio.h> int main() { int x = 22543; // variable int* pX = &x; // `pX` is pointer to `x` printf("The value of `pX` is %p\n", pX); // value of pointer `pX` printf("The value of `x` by pointer `pX` is %d\n", *pX); // pointer dereference return 0; }
Note
*(&variable) == *pVariable
If you attempt to dereference a null pointer, the compiler will raise an error:
Example provided to showcase the mistake
Main.c
12345678910#include <stdio.h> int main() { int* pX; printf("x = %p", pX); return 0; }
Swipe to start coding
Determine the size of int
and double
pointers.
Oplossing
Bedankt voor je feedback!
single
Awesome!
Completion rate improved to 2.63
Pointers
Veeg om het menu te tonen
Using the reference operator &
and the dereference operator *
, we can create and work with pointers.
A pointer is a data type, just like int
, char
, or double
.
The pointer is designed to store an address, which you can obtain using the &
operator.
To declare a pointer, prepend it with the *
character.
int* intPointer; // pointer to an int variable
double* doublePointer; // pointer to a double variable
char* charPointer; // pointer to a char variable
In essence, a pointer is a variable that holds the address of another object.
int x = 100; // variable
int* pX; // pointer to an int variable
pX = &x; // pX now points to x
Note
Typically, pointers are named by prefixing the letter
p
to the name of the object they're pointing to.
When you dereference a pointer, you access the value of the variable it points to.
Main.c
1234567891011121314#include <stdio.h> int main() { int x = 22543; // variable int* pX = &x; // `pX` is pointer to `x` printf("The value of `pX` is %p\n", pX); // value of pointer `pX` printf("The value of `x` by pointer `pX` is %d\n", *pX); // pointer dereference return 0; }
Note
*(&variable) == *pVariable
If you attempt to dereference a null pointer, the compiler will raise an error:
Example provided to showcase the mistake
Main.c
12345678910#include <stdio.h> int main() { int* pX; printf("x = %p", pX); return 0; }
Swipe to start coding
Determine the size of int
and double
pointers.
Oplossing
Bedankt voor je feedback!