Cainiao talks about two-dimensional arrays, and cainiao two-dimensional arrays.

Source: Internet
Author: User

Cainiao talks about two-dimensional arrays, and cainiao two-dimensional arrays.

Every time I encounter a two-dimensional array in the C language, it is a special headache. It is not difficult to use it, but I don't know what its array name represents. But we know that when we define an array, its array name is the address of the first element, that is, the following statement is equivalent:

int a[3]={1,2,3};printf ("%d\n",a);printf ("%d\n",&a[0]);

They will output the same value, and more importantly, they have the same meaning. In the process of accessing members from arrays, it is completely converted into pointers for indirect access, that is:

A [I] is equivalent to * (a + I) and a [I] is equivalent to (a + I)

Based on this idea, let's look at the two Arrays:

Int a [3] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}; printf ("% u \ n ", a); // The address is output using % u to prevent the address from being too large. Printf ("% u \ n", a [0]); printf ("% u \ n", & a [0]);

In a two-digit array, the values of the output result are the same, but only the values are the same. Their meanings are quite different. So who is the equivalent of the Two-dimensional array name above? The answer is & a [0]; // line address

In fact, the C language does not have a two-dimensional array, but the C language has a one-dimensional array, so it is not difficult to imitate an n-dimensional array. Therefore, a two-dimensional array is only an array, but no element in the array is also an array.

That is, each element a [I] represents an array. It is not difficult to understand it with a one-dimensional array. As mentioned above, in a one-dimensional array, the first address of the array name and the first element is equivalent to a <--> & a [0]; (one-dimensional array ).

So, every element of our array (two-dimensional array) is an array,

Therefore, a [0] <--> & a [0] [0]; // This indicates the column address, which will be differentiated from the row address.

So a <--> & a [0]; // The line address is displayed here.

Now we know the meaning of the two-dimensional array name, which is equivalent to that. Now let's look at the difference between the row address and column address.

In fact, in the above two-dimensional array code, the output results are the same, but their meanings are quite different. Different meanings? What is the difference? The difference is: Moving the pointer.

We know that the pointer is moved in bytes of the data type. The two pointers may point to the same address, but their unit is likely to be different.

int b[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};printf ("%u\n",b+1);//+16printf ("%u\n",b[0]+1);//+4printf ("%u\n",&b[0]+1);//+16

We can see that the distance between pointer movement is very different (I think this is very important ). This is because B and & B [0] are equivalent, and they represent the row address, that is, the distance of a row is moved every time one unit is moved.

B [0] is equivalent to & B [0] [0]. It indicates the column address. Each time it is moved, only the byte of sizeof (B [0] [0]) is moved, it is the byte occupied by an element, which is a difference in their meanings.

 

When I used the memset function, I was most afraid to use a two-dimensional array. I really don't know if it was to input a, a [0], & a [0], or & a [0] [0]. We know that their values are the same, but they mean different meanings. Which one do we need to input? In fact, everything works. This is no longer a knowledge of two-dimensional arrays. It is related to the void * pointer and the internal processing method of the memset function,

Void * memset (void * dest, int c, size_t count); // declaration of the memset Function

Void * pointer represents an address. It is just an address. We will not comment on their meaning. Why? Didn't the pointer mean very important? Now I don't know what to do? This is because the memset function can set any memory to a certain value. We can pass int *, double *, float *, char * or even struct _ Person *. It is impossible for us to write a function for each type, because after we have the struct type, the data type is no longer elusive. Therefore, to achieve this, we can only pass in one address, and process each byte (because the computer is byte ). I will paste the code below. It is no longer a knowledge of two-dimensional arrays.

Void * mymemset (void * str, int number, int ByteSize) {char * ptr = (char *) str; int I; for (I = 0; I <ByteSize; I ++) {* ptr = number; ptr ++;} // * ptr = '\ 0'; // after calling memset, if it is a string, remember to set the terminator return str ;}

The above is only my personal opinion. If there is any mistake, I hope readers will point out that the author is grateful.

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.