Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Working with Pointers | Pointers
C Basics

bookWorking with 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.

main.c

main.c

copy
123
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.

main.c

main.c

copy
123
int x = 100; // Variable int* pX; // Pointer to an `int` variable pX = &x; // `pX` now points to `x`
Note
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

Main.c

copy
12345678910111213
#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
Note

*(&variable) == *pVariable

If you attempt to dereference a null pointer, the compiler will raise an error:

Main.c

Main.c

copy
1234567
#include <stdio.h> int main() { int* pX; printf("x = %p", pX); return 0; }
Task

Swipe to start coding

Determine the size of int and double pointers.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 3
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

What is the difference between referencing and dereferencing a pointer?

Can you give an example of how to declare and use a pointer?

Why is dereferencing a null pointer an error?

close

Awesome!

Completion rate improved to 2.63

bookWorking with Pointers

Swipe to show menu

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.

main.c

main.c

copy
123
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.

main.c

main.c

copy
123
int x = 100; // Variable int* pX; // Pointer to an `int` variable pX = &x; // `pX` now points to `x`
Note
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

Main.c

copy
12345678910111213
#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
Note

*(&variable) == *pVariable

If you attempt to dereference a null pointer, the compiler will raise an error:

Main.c

Main.c

copy
1234567
#include <stdio.h> int main() { int* pX; printf("x = %p", pX); return 0; }
Task

Swipe to start coding

Determine the size of int and double pointers.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 3
single

single

some-alt