Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Pointer Declaration and Dereferencing | Pointer Fundamentals
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Pointers Mastery

bookPointer 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

main.c

copy
12345678910111213141516171819202122
#include <stdio.h> int main() { int num = 42; int *ptr; // Assign the address of num to ptr ptr = &num; // 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
Note
Note

The * operator declares a pointer and also dereferences it to access the value stored at its address.

question mark

Which of the following statements correctly declare a pointer to an int?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookPointer 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

main.c

copy
12345678910111213141516171819202122
#include <stdio.h> int main() { int num = 42; int *ptr; // Assign the address of num to ptr ptr = &num; // 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
Note
Note

The * operator declares a pointer and also dereferences it to access the value stored at its address.

question mark

Which of the following statements correctly declare a pointer to an int?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
some-alt