Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Pointers Brief Overview | Working with Pointers and Structs
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Mastering C Structs

bookPointers Brief Overview

Dereferencing Operator

A pointer is a variable that contains the address of another object.

Dereference operator "*" returns the value stored at the address.

Declaring and initializing a pointer looks like this:

int* pName = NULL;
main.c

main.c

copy
12345678
#include <stdio.h> int main() { int* pName = NULL; printf("%p\n", pName); // `%p` is a specifier for a pointer return 0; }

Referencing Operator &

The & operator returns the address of an object:

main.c

main.c

copy
12345678
#include <stdio.h> int main() { int variable; printf("%p\n", &variable); return 0; }

A variable of pointer type is used to store the address, which is returned by the & operator.

int variable;
int* pVariable;
pVariable = &variable;

To unpack the contents at the specified address, you must use the * operator on a variable of type pointer.

main.c

main.c

copy
12345678910
#include <stdio.h> int main() { int variable = 1024; int* pVariable = &variable; printf("Address: %p\n", pVariable); // `%p` specifier for a pointer printf("Returned value by address: %d\n", *(pVariable)); // using `*` to pointer return 0; }
Task

Swipe to start coding

You have a product with a specific price, and you need to increase its value by a given percentage.
Your task is to implement a function that updates the product's price using a pointer.

Inside the updatePrice function:

  1. Use the dereference operator (*) to access the current value stored in memory β€” for example, *price refers to 100.0.
  2. Calculate the percentage of the current price β€” *price * percentIncrease / 100.0, which for 10% of 100.0 equals 10.0.
  3. Add this percentage to the original value to get the updated price β€” *price = *price + (percentage part).
  4. The function doesn't return anything β€” it directly modifies the value through the pointer.

Example

Initial PriceIncrease (%)Updated Price
100.010.0110.0
250.05.0262.5
80.025.0100.0

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
single

single

Ask AI

expand

Ask AI

ChatGPT

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

Suggested prompts:

Can you explain the difference between the * and & operators in more detail?

Can you give an example of how to use pointers in a function?

What are some common mistakes when working with pointers?

close

bookPointers Brief Overview

Swipe to show menu

Dereferencing Operator

A pointer is a variable that contains the address of another object.

Dereference operator "*" returns the value stored at the address.

Declaring and initializing a pointer looks like this:

int* pName = NULL;
main.c

main.c

copy
12345678
#include <stdio.h> int main() { int* pName = NULL; printf("%p\n", pName); // `%p` is a specifier for a pointer return 0; }

Referencing Operator &

The & operator returns the address of an object:

main.c

main.c

copy
12345678
#include <stdio.h> int main() { int variable; printf("%p\n", &variable); return 0; }

A variable of pointer type is used to store the address, which is returned by the & operator.

int variable;
int* pVariable;
pVariable = &variable;

To unpack the contents at the specified address, you must use the * operator on a variable of type pointer.

main.c

main.c

copy
12345678910
#include <stdio.h> int main() { int variable = 1024; int* pVariable = &variable; printf("Address: %p\n", pVariable); // `%p` specifier for a pointer printf("Returned value by address: %d\n", *(pVariable)); // using `*` to pointer return 0; }
Task

Swipe to start coding

You have a product with a specific price, and you need to increase its value by a given percentage.
Your task is to implement a function that updates the product's price using a pointer.

Inside the updatePrice function:

  1. Use the dereference operator (*) to access the current value stored in memory β€” for example, *price refers to 100.0.
  2. Calculate the percentage of the current price β€” *price * percentIncrease / 100.0, which for 10% of 100.0 equals 10.0.
  3. Add this percentage to the original value to get the updated price β€” *price = *price + (percentage part).
  4. The function doesn't return anything β€” it directly modifies the value through the pointer.

Example

Initial PriceIncrease (%)Updated Price
100.010.0110.0
250.05.0262.5
80.025.0100.0

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Β 2. ChapterΒ 1
single

single

some-alt