Pointers and Arrays (i)

Source: Internet
Author: User
Tags define arrays variables printf reference

Variables have addresses in memory storage, and arrays also have addresses in memory. Array names are the first addresses that an array is placed in memory. A pointer variable is the address that holds the variable, which can point to a variable, and of course the address of the array's first or array element, that is, the pointer variable can point to an array or array element, and the reference to arrays and array elements can also be used with pointer variables. The following is a description of the pointers and different types of arrays respectively.

6.4.1 pointers and one-dimensional arrays

Suppose we define a one-dimensional array in memory that has a system-allocated storage space whose array name is the first address of the array in memory. If you define a pointer variable and pass the first address of the array to the pointer variable, the pointer points to the one-dimensional array. We say that the array name is the first address of the array, which is the pointer to the array. The defined pointer variable is the pointer variable that points to the array. A reference to a one-dimensional array can be used either by the subscript method of the traditional array element or by the representation of the pointer.

int a[10],*ptr;/* definition array and pointer variable * *

To do assignment operations:

Ptr=a; or ptr=&a[0];

Then PTR gets the first address of the array. Where A is the first address of the array, &a[0] is the address of the array element a[0], because the address of a[0] is the first address of the array, so the two assignment effects are exactly the same. The pointer variable ptr is the pointer variable that points to array a.

If PTR points to one-dimensional arrays, now look at the representation of the set of pointers in C:

1) ptr+n and a+n represent the address of the array element A[n], that is, &a[n]. For the entire a array, there are 10 elements, and the value of n is 0~9, the address of the array element can be expressed as ptr+0~ptr+9 or a+0~a+9, consistent with &a[0]~&a[9.

2) Knowing the address representation of an array element, * (Ptr+n) and * (A+N) indicate that the elements of the group are equivalent to A[n].

3 The pointer variable pointing to the array can also be represented as ptr[n with the subscript form of the array, and the effect is equivalent to * (Ptr+n).

[Example 6-5]/* the following standard method input output array elements.

The following 10 numbers are entered from the keyboard, outputting the values of each element of the array as a different reference to the array.

#include <stdio.h>
main()
{
int n,a[10],*ptr=a;
for(n=0;n<=9;n++)
scanf("%d",&a[n]);
printf("1------output!\n");
for(n=0;n<=9;n++)
printf("%4d",a[n]);
printf("\n");
}

To run the program:

RUN
1234567890¿
1------output!
1234567890

[Example 6-6] uses the address method represented by the pointer variable to input the elements of the output array.

#include <stdio.h>
main()
{
int n,a[10],*ptr=a;/*定义时对指针变量初始化*/
for(n=0;n<=9;n++)
scanf("%d",ptr+n);
print f("2------output!\n");
for(n=0;n<=9;n++)
print f("%4d",*(ptr+n));
print f("\n");
}

To run the program:

RUN
1234567890¿
2------output!
1234567890

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.