English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structures

C Language Files

C Others

C Language Reference Manual

C program uses pointers to access array elements

Comprehensive Collection of C Programming Examples

In this example, you will learn how to use pointers to access array elements.

To understand this example, you should be familiar with the followingC programming languageTopic:

Using pointers to access array elements

#include <stdio.h>
int main() {
    int data[5];
    printf("Input element: ");
    for (int i = 0; i < 5; ++i)
        scanf("%d", data + i);
    printf("You input: \n");
    for (int i = 0; i < 5; ++i)
        printf("%d\n", *(data + i));
    return 0;
}

Output result

Input element: 1
2
3
5
4
You input: 
1
2
3
5
4

In this program, the elements are stored in the integer array data[].

Then, use the pointer symbol to access the elements of the array. By the way,

  • data[0] is equivalent to* data, &data[0] is equivalent to data

  • data[1] is equivalent to*(data + 1) &data[1] is equivalent to data + 1

  • data[2] is equivalent to*(data + 2) &data[2] is equivalent to data + 1

  • ...

  • data[i] is equivalent to*(data + i), &data[i] is equivalent to data + i

Visit this page to learnRelationship between Pointers and Arrays.

Comprehensive Collection of C Programming Examples