Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Pointers and Arrays | Pointers in Arrays and Functions
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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookPointers and Arrays

Deslize para mostrar o 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
some-alt