Pointers Brief Overview
To go through the following chapters, I suggest briefly repeating the pointers in the C programming language.
If you are not familiar with the basic skills of working with the C programming language, we recommend taking our basic C programming language course: C Basic
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 |
Solución
¡Gracias por tus comentarios!
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Can you explain the difference between the * and & operators in C?
Can you give an example of how to use pointers in a function?
What are some common mistakes when working with pointers in C?
Awesome!
Completion rate improved to 4.17
Pointers Brief Overview
Desliza para mostrar el menú
To go through the following chapters, I suggest briefly repeating the pointers in the C programming language.
If you are not familiar with the basic skills of working with the C programming language, we recommend taking our basic C programming language course: C Basic
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 |
Solución
¡Gracias por tus comentarios!
single