Referencing an array element with a pointer and printing output

Source: Internet
Author: User
#include <stdio.h>void main () {int a[5]={10,20,30,40,50};int *aptr,i;//pointer variable declaration aptr=&a[0];//pointer variable points to variable afor (i= 0;i<5;i++)//referencing elements by array subscript printf ("a[%d]=%d\n", I,a[i]);    for (i=0;i<5;i++)//referencing elements by array name printf ("*a (a+%d) =%d\n", i,* (A+i));    for (i=0;i<5;i++)//By pointer variable subscript reference element printf ("aptr[%d]=%d\n", I,aptr[i]);    for (aptr=a,i=0;aptr<a+5;aptr++,i++)//By pointer variable offset reference element printf ("* (aptr+%d) =%d\n", i,*aptr);}
in the above program, there are four for loops, where the first for loop accesses the elements of an array using an array subscript, and the second for loop accesses the array elements using the array name. In C, an address can also be added and reduced as a general variable, but the addition of 1 and minus 1 of the pointer represents an element cell, the third for loop is the element that accesses the array with a pointer, and the fourth for loop offsets the pointer and then accesses the content that the pointer points to. 

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Referencing an array element with a pointer and printing output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.