29th lesson pointer and array analysis (bottom)

Source: Internet
Author: User

1. How arrays are accessed

(1) Accessing The elements in the array as subscripts: a[i];

(2) To access elements in the array as pointers : * (a+i)

2. subscript form VS pointer form

(1) When moving in an array with fixed increments in the form of pointers, the efficiency is higher than the subscript form

(2) Higher efficiency when the pointer increment is 1 and the hardware has a hardware increment model

(3) The conversion of subscript form and pointer form:a[n]==* (a+n) ==* (n+a) ==n[a];

▲ Note:

① Modern compiler's generation code optimization rate has been greatly improved, in fixed increments, the efficiency of subscript form is equivalent to the pointer form;

② but from the point of view of readability and code maintenance , the subscript form is more excellent

How the Instance analysis array is accessed

#include <stdio.h>intMain () {inta[5] = {0}; int* p =A; inti =0;  for(i=0; i<5; i++) {P[i]= i +1;//assigning values to array elements using pointers    }     for(i=0; i<5; i++)    {       //accessing array elements as pointersprintf"a[%d] =%d\n", i,* (A +i)); }     for(i=0; i<5; i++)    {       //access to array elements in the following formI[a] = i +Ten;//compare to the other, equivalent to a[i] = i + ten    }     for(i=0; i<5; i++)    {       //accessing array elements with pointersprintf"p[%d] =%d\n", I,p[i]); }      return 0;}

The "Programming experiment" array and pointers are different

Ext.c

// at the time of the experiment, this file should be. C, not a header file. Because header files are directly included in the file,// and. c files are compiled separately int a[5] = {123  45// in the file, a is treated as an array

29-2.c

#include <stdio.h>//command-line compilation of these two files: gcc 29-2.c ext.cintMain () {//A is defined as an array in an external file.     int a[5] = {1, 2, 3, 4, 5}; //Lab 1:    extern intA[];//This document, if so declared, a is still considered an arrayprintf ("&a =%p\n", &a);//The &a here is the address of the entire array.printf"A =%p\n", a);//a The address of the first element, which is numerically equal to &aprintf"*a =%d\n", *a);//print out the 1th element, which is 1//Lab 2:/*extern int* A;       If this is declared, the compiler uses a as an int//pointer. printf ("&a =%p\n", &a);    The address of pointer A is looked up from the symbol table, pointing to the array printf ("A =%p\n", a);  From pointer A, take an int type of data, that is, the 1th element, 1 printf ("*a =%d\n", *a); An attempt was made to remove data from address 0x01, violating memory access errors. */      return 0;}

3. The difference between a and &a

(1) A address to the first element of the array

(2) &a is the address of the entire array , and the array can be considered as a data node. such as int[5];

(3) The difference between a and &a in the pointer Operation

A + 1 = = (unsigned int) A + sizeof (*a),//a represents the first element address, and *a represents a 1th element

&a + 1 = = (unsigned int) (&A) + sizeof (*&a) = = (unsigned int) (&A) + sizeof (a);

The classic problem of "case analysis" pointer operation

4. Array Parameters

(1) When an array is a function parameter, the compiler compiles it to the corresponding pointer. Therefore, in general, when you have an array parameter in a defined function, you need to define another parameter to indicate the size of the array.

void f (int a[]) is equivalent to void f (int* a);

void f (int a[5]) is equivalent to void f (int* a); is a pointer, missing information about the length of the array

"Instance analysis" Unreal array parameters

5. Summary

(1) The array name and pointer are only used the same way , the essence of the array name is not a pointer, the nature of the pointer is not an array.

(2) The array name is not the address of the array, but the address of the first element of the array.

(3) The array parameter of the function is degraded to a pointer

29th lesson pointer and array analysis (bottom)

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.