Pointers 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
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
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
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; }
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:
- Use the dereference operator (
*) to access the current value stored in memory β for example,*pricerefers to100.0. - Calculate the percentage of the current price β
*price * percentIncrease / 100.0, which for 10% of 100.0 equals10.0. - Add this percentage to the original value to get the updated price β
*price = *price + (percentage part). - The function doesn't return anything β it directly modifies the value through the pointer.
Example
| Initial Price | Increase (%) | Updated Price |
|---|---|---|
| 100.0 | 10.0 | 110.0 |
| 250.0 | 5.0 | 262.5 |
| 80.0 | 25.0 | 100.0 |
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.35
Pointers 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
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
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
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; }
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:
- Use the dereference operator (
*) to access the current value stored in memory β for example,*pricerefers to100.0. - Calculate the percentage of the current price β
*price * percentIncrease / 100.0, which for 10% of 100.0 equals10.0. - Add this percentage to the original value to get the updated price β
*price = *price + (percentage part). - The function doesn't return anything β it directly modifies the value through the pointer.
Example
| Initial Price | Increase (%) | Updated Price |
|---|---|---|
| 100.0 | 10.0 | 110.0 |
| 250.0 | 5.0 | 262.5 |
| 80.0 | 25.0 | 100.0 |
Solution
Thanks for your feedback!
single