Empire C:basic 3

Source: Internet
Author: User
Tags array definition

First we define a pointer that represents the age:

int* page; This is the definition of a pointer, and the definition of a normal variable is more than a * symbol.

Why does the variable name start with P, which refers to the English pointer (point), which means that it is a pointer, not a normal variable, plus no p does not matter.

The 1.* is the value operator (indirect access), which is not the meaning of multiplication sign.

The 2.* must be placed in the middle of the pointer type and pointer variable, where it doesn't matter

There are 3 ways: int* page; int *page; int * page;

The first is a more formal way of writing, the 2nd is the most used, but it is best not to use the 3rd.

First, clear several concepts:

1. Pointers are also a data type

int* here means that a pointer to an int type variable is defined, not a pointer that defines an int type

int* page Here the page is a pointer variable, it can only store the address, C language, only the pointer type of variables to store the address, not you can simply define a variable to be stored.

2. Two functions when defining pointers

int *page=&age; not only gets the address of age, but also gets the value of the age variable.

3. The pointer must be initialized before it is used, and the pointer type is consistent with the pointer to the type.

4. Address

Each variable has its own address, and it is unique and distinct, and the addresses are not necessarily all numbers.

5. Direct Access and indirect access

    • Direct access: Access variable values by variable address
    • Indirect access: Access variables by storing variable address variables
Second, pointer array & array pointers

1. Pointer and array relationships

The array name is a pointer to the address

int A[5] This is an array of type int

It has 5 elements, respectively, a[0],a[1],a[2],a[3],a[4], and these 5 are variables, not much different from normal variables.

The name of this array is a, but don't make it into a[5].

A is a pointer constant (the address cannot be changed, cannot point to another variable), it only stores the address of the first element of the array, that is, a[0]

So we don't need & when we use scanf () to input strings.

Char A[5]

scanf ("%s", a)

Why is the address of the first element stored only, and what about the other elements?

In C, the memory allocation of an array is continuous, that is, if the address of the first element of the array is 1000, then the 2nd is 1001, so if you know the address of the first element, you can know the address of the other element.

2. Pointer array & Array pointers

Pointer array: array of pointers, which is used to store pointers to arrays, which are arrays of elements that are pointers

Array pointer: A pointer to an array, which is a pointer to the array

Also note the differences in their usage, as illustrated below.

int* a[4] Pointer array

Expression: The elements in array A are INT-type pointers

Element means: *a[i] * (A[i]) is the same, because [] priority is higher than *

int (*a) [4] Array pointer

Represents: Pointer to array a

Element representation: (*A) [i]

Note: In practical applications, for pointer arrays, we often use this:

1 int* pInt; 2 pInt a[4];
View Code

This is the same as the meaning expressed in the pointer array definition above, except that the type transformation is taken.

The code demonstrates the following:

1Include <stdio.h>2 3 intMain ()4 {5 intc[4]={1,2,3,4};6 int*a[4];//Array of pointers7 int(*B) [4];//Array Pointers8b=&C;9 //assigning an element in array C to array aTen  for(intI=0;i<4; i++) One { Aa[i]=&C[i]; - } - //output look at the results theprintf"%d", a[1]) L;//Output 2 is the right -printf"%d", (*B) [2]);//Output 3 is the right - return 0; -}
View CodeReference documents:

1. Differences between pointer arrays and arrays of pointers

2. Learn programming from scratch---the first step-C language

Empire C:basic 3

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.