Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Pointers and Arrays | Pointers in Arrays and Functions
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Pointers Mastery

bookPointers and Arrays

When you declare an array in C, such as int numbers[5];, the array name (numbers) represents the address of its first element. This means that numbers can be used like a pointer of type int *. You can assign the address of numbers to a pointer variable, and then use that pointer to access or modify the elements of the array.

main.c

main.c

copy
123456789101112131415161718192021222324
#include <stdio.h> int main() { int numbers[5] = {10, 20, 30, 40, 50}; int *ptr = numbers; // Array name acts as a pointer to the first element printf("Accessing array using array indexing:\n"); for (int i = 0; i < 5; i++) { printf("numbers[%d] = %d\n", i, numbers[i]); } printf("\nAccessing array using pointer arithmetic:\n"); for (int i = 0; i < 5; i++) { printf("*(ptr + %d) = %d\n", i, *(ptr + i)); } printf("\nModifying array elements using pointers:\n"); for (int i = 0; i < 5; i++) { *(ptr + i) = *(ptr + i) + 1; // Increment each element by 1 printf("numbers[%d] after increment = %d\n", i, numbers[i]); } return 0; }

Array indexing and pointer arithmetic are closely related. Accessing numbers[i] is equivalent to using pointer arithmetic as *(numbers + i). Both expressions retrieve the value at the ith position of the array. Pointer arithmetic adds the offset (in elements, not bytes) to the base address of the array, which makes *(numbers + i) point to the correct element.

Using array indexing is typically clearer and more readable for most array operations, especially when you want to emphasize working with arrays. However, pointer arithmetic can be useful in situations where you need to traverse arrays efficiently, such as in performance-critical code or when writing functions that operate on arrays via pointers. Both techniques are interchangeable when accessing or modifying elements, but readability and code clarity should guide your choice in most programs.

question mark

Which statement about the relationship between arrays and pointers in C is correct?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookPointers and Arrays

Scorri per mostrare il menu

When you declare an array in C, such as int numbers[5];, the array name (numbers) represents the address of its first element. This means that numbers can be used like a pointer of type int *. You can assign the address of numbers to a pointer variable, and then use that pointer to access or modify the elements of the array.

main.c

main.c

copy
123456789101112131415161718192021222324
#include <stdio.h> int main() { int numbers[5] = {10, 20, 30, 40, 50}; int *ptr = numbers; // Array name acts as a pointer to the first element printf("Accessing array using array indexing:\n"); for (int i = 0; i < 5; i++) { printf("numbers[%d] = %d\n", i, numbers[i]); } printf("\nAccessing array using pointer arithmetic:\n"); for (int i = 0; i < 5; i++) { printf("*(ptr + %d) = %d\n", i, *(ptr + i)); } printf("\nModifying array elements using pointers:\n"); for (int i = 0; i < 5; i++) { *(ptr + i) = *(ptr + i) + 1; // Increment each element by 1 printf("numbers[%d] after increment = %d\n", i, numbers[i]); } return 0; }

Array indexing and pointer arithmetic are closely related. Accessing numbers[i] is equivalent to using pointer arithmetic as *(numbers + i). Both expressions retrieve the value at the ith position of the array. Pointer arithmetic adds the offset (in elements, not bytes) to the base address of the array, which makes *(numbers + i) point to the correct element.

Using array indexing is typically clearer and more readable for most array operations, especially when you want to emphasize working with arrays. However, pointer arithmetic can be useful in situations where you need to traverse arrays efficiently, such as in performance-critical code or when writing functions that operate on arrays via pointers. Both techniques are interchangeable when accessing or modifying elements, but readability and code clarity should guide your choice in most programs.

question mark

Which statement about the relationship between arrays and pointers in C is correct?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1
some-alt