[Good programmer notes sharing] -- array and pointer, programmer array pointer

Source: Internet
Author: User

[Good programmer notes sharing] -- array and pointer, programmer array pointer

Ios training ------ my C Language notes. I look forward to communicating with you!

 

We have introduced the issue of memory allocation in C language. Next we will introduce two important knowledge points about C language: array and pointer.

 

Arrays and pointers are not only in the C language, but also in the OC (of course, the OC has helped us solve the problem internally, but there are still a large number of pointers). in C, it is very common to wait for some development in C ++, so as a programmer, it is necessary to master.

 

1. array: Set of the same type

1: One-dimensional array

We usually declare a variable. If there are many variables, how can we do this? In this case, we can use arrays. What is an array?

First, let's look at an instance:

# Include <stdio. h>

 

Int main (void)

{

Int a [20];

Here we call a an array of 20 elements. Each element is your listening type. Of course, you can declare it as another type (Note: This is a one-time Declaration)

The numbers in [] indicate the following table: a [0]... A [19]

Return 0;

}


Arrays are the integration of data.

For example, the definition format of an integer array is as follows:

Int variable name [number of elements];

For example: int a [10]; then how to store the data in an integer array. There are several ways to store the array:

  • Value int a [5] = {0, 1, 2, 3} when defining}
  • Assign a single value to int B [9] = {[4] = 5, [7] = 2} during definition };
  • Int c [6]; a [2] = 1; a [3] = 4;

Since data can store multiple data, it is stored in the memory as follows:

Note: arrays cannot be sorted and assigned values, for example, a = B;

Here is an example:

Int main ()
{
Char cs [5] = {'1', '2', '3', '4', '5 '};

Return 0;
}

 

Note: memory allocation to arrays is also distributed from large to small Based on occupied bytes, storing data from top to bottom in sequence, and the cs address is its first address, that is, cs [0]. it is similar to storing variables, but it is not converted to binary.

 

2: Two-dimensional array

In fact, if arrays are used to store data, there are inevitably some shortcomings. Then there is a two-dimensional array. The two-dimensional number is an array or two-dimensional arrays.

A two-dimensional array can store more data than a one-dimensional array, which is actually two one-dimensional numbers. The input and output methods of arrays can be implemented using a for Loop:

 

 

 

3: Introduction to sizeof and strlen in the array

Sizeof: Calculate the byte count as '\ 0'

Strlen: returns the length of a string. the header file string. h must be added.

Here is an example:

See the following code:

Int I, a [] = {3, 4, 5, 6, 7, 3, 7, 4, 4 };
For (I = 0; I <= 9; I ++)
{
Printf ("% d", a [I]);
}

Obviously, it shows the element values of array.

We can also access the element as follows:

Int I, a [] = {3, 4, 5, 6, 7, 3, 7, 4, 4 };
For (I = 0; I <= 9; I ++)
{
Printf ("% d", * (a + I ));
}

The results and functions are exactly the same.

 

2. pointer: (a variable)

The pointer is an 8-Byte variable,

# Include <stdio. h>

 

Int main (void)

{

Int;

Int * p = & a; // get the address character and put the address of the cockroach in p!

Printf ("% ld \ n", sizeof (o), sizeof (int *));

Return 0;

}

The core of C language is pointer, which can operate memory and indirectly modify data in memory.

Pointer type:

1. An array pointer, as its name implies, defines a pointer. The value stored in the pointer is the address of the array.

 

}

 


2. The character pointer is the pointer of a single character. It focuses on the string pointer, that is, the character array pointer. However, it can be completely replaced by a pointer during definition. For example:


Char * name = "minglei"; // String constant

The general definition is:

Char name [10]; // string variable

Char * n = name;

 

We define a pointer to the pointer;

  • Int a = 5; the data type of variable a is int
  • Int * p = a; the data type of pointer p is int *
  • Int ** pp = p; the data type of pointer pp is int **

It is easy to understand that (data type *) is one.

3. Differences between array names and pointer Variables

See the following code:

Int I, * pa, a [] = {3, 4, 5, 6, 7, 3, 7, 4, 4 };
Pa =;
For (I = 0; I <= 9; I ++)
{
Printf ("% d", * pa );
Pa ++; // note that the pointer value is modified.
}

As you can see, this code also outputs the values of each element in the array. However, try changing the pa in {} to. You will find that the program compilation fails. It seems that the pointer and array name are still different. In fact, the pointer above is a pointer variable, while the array name is only a pointer constant. The difference between this code and the above Code is that the value of pointer pa is continuously increasing throughout the cycle, that is, the pointer value is modified. The array name is a pointer constant and its value cannot be modified. Therefore, it cannot be operated like this: a ++. at pa [I] And * (pa + I) in section 4 and 5 above, the value of pointer pa is to make the final change. So the variable pointer pa and array name a can be exchanged.

Note: The pointer can be added or subtracted.

Use arrays and pointers in combination:

Pointer Array
A pointer array means that each element in an array is a pointer. For example:

Int * p [10]; // instead of int (* p) [10]

Or

Char * p [10];

P is a pointer (the value is the same as & p [0 );
Int t [10];

Int * pt = t; // Use pt to point to t

So what do we use here to point to t in int * t [10? We want to use a pointer:

Int ** pt = t;

This is because, in int * t [10], each element is a pointer, and t points to this array, which is the same as & t [0, that is, pointing to t [0], pointing to a pointer variable, can be said to be a pointer, so naturally we need

Int ** pt;

Here I use a graph to figure out the array and pointer:

If you can understand this (what is the result ?), Then you get started with arrays and pointers. Remember the knowledge!

Related Article

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.