Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Pointer Declaration and Dereferencing | Pointer Fundamentals
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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you explain more about how pointers work in C?

What happens if I try to dereference an uninitialized pointer?

Why is it important to use NULL for uninitialized pointers?

bookPointer Declaration and Dereferencing

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 2
some-alt