Pointer Declaration and Dereferencing
In C, understanding the difference between a value and an address is essential when working with pointers. A variable holds a value directly, such as int age = 30;, where age contains the value 30, while a pointer holds the memory address of a variable, such as int *p;, where p can store the address of an int variable. Suppose you have this code:
int number = 42;
int *ptr = &number;
Dereferencing means accessing the value stored at the memory address held by a pointer. Use the * operator to do this:
int value = *ptr; // value is now 42
Here, *ptr gives you the value stored at address 0x1000, which is 42.
main.c
12345678910111213141516171819202122#include <stdio.h> int main() { int num = 42; int *ptr; // Assign the address of num to ptr ptr = # // Print the address stored in ptr printf("Address of num: %p\n", (void*)ptr); // Print the value of num using the pointer printf("Value of num via ptr: %d\n", *ptr); // Modify the value of num through the pointer *ptr = 100; // Print the modified value of num printf("Modified value of num: %d\n", num); return 0; }
Uninitialized pointers are a frequent source of bugs in C. If you declare a pointer but do not assign it a valid address, it points to an unknown memory location. Using such a pointer can cause your program to crash or behave unpredictably.
int *ptr; // Declared but not initialized
*ptr = 42; // Dangerous: writing to an unknown address
To avoid this mistake always initialize pointers when you declare them. Assign them either the address of a variable or NULL if you do not have an address yet.
int value = 10;
int *ptr = &value; // Safe: initialized with address
The * operator declares a pointer and also dereferences it to access the value stored at its address.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Incrível!
Completion taxa melhorada para 9.09
Pointer Declaration and Dereferencing
Deslize para mostrar o menu
In C, understanding the difference between a value and an address is essential when working with pointers. A variable holds a value directly, such as int age = 30;, where age contains the value 30, while a pointer holds the memory address of a variable, such as int *p;, where p can store the address of an int variable. Suppose you have this code:
int number = 42;
int *ptr = &number;
Dereferencing means accessing the value stored at the memory address held by a pointer. Use the * operator to do this:
int value = *ptr; // value is now 42
Here, *ptr gives you the value stored at address 0x1000, which is 42.
main.c
12345678910111213141516171819202122#include <stdio.h> int main() { int num = 42; int *ptr; // Assign the address of num to ptr ptr = # // Print the address stored in ptr printf("Address of num: %p\n", (void*)ptr); // Print the value of num using the pointer printf("Value of num via ptr: %d\n", *ptr); // Modify the value of num through the pointer *ptr = 100; // Print the modified value of num printf("Modified value of num: %d\n", num); return 0; }
Uninitialized pointers are a frequent source of bugs in C. If you declare a pointer but do not assign it a valid address, it points to an unknown memory location. Using such a pointer can cause your program to crash or behave unpredictably.
int *ptr; // Declared but not initialized
*ptr = 42; // Dangerous: writing to an unknown address
To avoid this mistake always initialize pointers when you declare them. Assign them either the address of a variable or NULL if you do not have an address yet.
int value = 10;
int *ptr = &value; // Safe: initialized with address
The * operator declares a pointer and also dereferences it to access the value stored at its address.
Obrigado pelo seu feedback!