Array of C

Source: Internet
Author: User

In C, in almost all expressions using array names, array names are processed as constant pointers, except for the following two cases:

 

1. sizeof

The value of sizeof (array name) is the size of the entire array, rather than the size of the pointer.

2. When the array name is used as the & operand

& The array name indicates a pointer to an array.

Therefore, the sizeof of the Two-dimensional array char a [2] [4] is 2*4. The form parameter that a can assign or the type that can be converted is Char (const *) [4]. that is, a constant pointer pointing to a char [4] array, instead of a char **. because after a is converted to a pointer, it has various operations on the pointer. If the value is Char ** P = A, in P ++, the increment of P is sizeof (char
*); If the value is Char (* P) [4] = A, then when P ++ is used, the increment of P is sizeof (char [4]). obviously, the latter is correct. P can correctly point to the next one-dimensional array (the element of the Two-dimensional array is a one-dimensional array ). in fact, the compiler does not allow assignments from A to Char.

 

Sizeof usage of two-dimensional arrays and two-dimensional pointers

char char_array[3][10] = {"a.txt", "b.txt"};printf("char_array's sizeof = %d\n", sizeof(char_array));printf("*char_array's sizeof = %d\n", sizeof(*char_array));printf("**char_array's sizeof = %d\n", sizeof(**char_array));char **char_array2 = {"a.txt", "b.txt"};printf("char_array2's sizeof = %d\n", sizeof(char_array2));printf("*char_array2's sizeof = %d\n", sizeof(*char_array2));printf("**char_array2's sizeof = %d\n", sizeof(**char_array2));

The output is as follows:

Char_array's sizeof = 30 // Level 2 pointer 3*10 * char_array's sizeof = 10 // in 10 * char_array's sizeof = 1 // directed to “a.txt achar_array2's sizeof = 4 // pointer size * char_array2's sizeof = 4 // pointer size ** char_array2's sizeof = 1 // A in character a.txt

 

 

In-depth analysis of arrays in C Language
int   main () {     char  a[2][3]={ {1,2,3} ,{4,5,6} } ;      char   (*p) [3];       p=a;     printf (“%d , %d , %d , %d ,%d ”, a , &a, *a, **a,  *(*(p+1) + 2));    return   0; } 
Output: 624706384,624 706384, 624706384, 1, 6

First, let's discuss this question: char * PC. Suppose the value in PC is 0x00012. What if PC ++ is used? It is obviously 0x00013, right. If it is int * pint; if the value of pint is 0x00012, then the value after pint ++ should be 0x00016 ...... you can simply understand this problem as a result of address alignment! I just want to say that if you want to see how many bytes a pointer points to, you can use this method. Next we will use this method to explain the following issues.

 

In the first sentence, this sentence is obviously okay. We all know that this is just to define a two-dimensional array, two rows and three columns, and assign values to them respectively. As you can see, a is the first address of this array. Every teacher in this book said this, although I don't know why ......

 

In the second sentence, it seems a bit like an array, but there is a * P in the brackets. It seems to be rare. Don't worry, you can think of it as char * P [3]; you can see it with your eyes, but it just removes the brackets. Well, I will look at the sentence without parentheses above. Obviously, most people will know at a glance that this is actually a pointer array, that is: (char *) P [3]; it is no different from a common array, except that each member in this array is a char * character pointer. That's right. But what about the brackets? Char
(* P) [3] What does it mean? Well, if you start to think it is a bit vague, you are normal, because it is also true at the beginning. Let's look at the following two sentences: typedef char _ char [3]; _ char * P; what is P in this sentence? Maybe you are not very clear, but you should be sure to be a pointer of some type. If you do not know much about the usage of typedef here, you may need to go to Baidu for usage of typedef, I will only tell you that the P here is exactly the same as the char (* P) [3] above. That is to say, this is not an array, but a pointer. The Pointer Points to an array with three elements instead of a common char. You can try the value of sizeof (P). The result should be 4. Will it be an array? You can also output the values of P and P ++ to see their differences? It should be 3!

 

This third sentence is simpler. A simple value assignment statement assigns a second-level pointer A (the first address of a two-dimensional array) to P (also a second-level pointer ).

 

The fourth sentence is an output statement. Let's look at the following parameters. The value of A should be the first address of the array. & A seems to have no such usage. Have you ever seen it? If you suspect that the compiler will report an error, you can try it. At least I can use it on vc6.0. A itself is an address. What will it be if I get another address? Maybe you think so. It's not surprising. I think so too. Let's take a look at * A. It can be understood that A is an address. * A is the value in the address. It seems that this is true.

 

Well, the above analysis of the small program is just an introduction. The next step is what I really want to talk about. After learning the C language, how many data types are there? From int, short, long, double, Char, char *, to char a [3], to int AA [5], and then to struct
A {int A, char C;} AA; we can see that there are three types, the first is the most basic type, such: int, short, cha, etc ...... the second is extended in the most basic type, that is, an array consisting of multiple identical types. The third is composed of different types, that is, struct.

 

Let's take a look at the following situation: int A; char B [5]; struct a AA; as for what this is, maybe you should check tan haoqiang's C language. So what are the values of A, B, AA, and? Of course, I don't care about its true values here. I just want to explain what they represent! Obviously, the value of a is the content of the four bytes allocated by the compiler in the stack. AA is similar, but what about B? B should also represent the content in the five bytes, that is, B should represent the content in the space from B [0] to B [4, however, it must be noted that it is an array, so the compiler actually provides a separate exception: When an array name is referenced somewhere, it does not represent all the content in the array, it represents the first address of the array. This note: when using the array name, we should note that it is generally understood as an address. If it is in the form of "& array name", we can also regard the array name as the overall content of the array.
. We can keep up with the comparison between A and AA. For ordinary A, it is obvious that when referencing a, we naturally think it is the content contained in the space, when we reference the struct variable name, we naturally regard it as the content in the space occupied by the struct. For arrays, according to the thinking logic we just described, the array name actually represents the content actually contained in the array, rather than its first address. Yes, we should have understood this. But the actual situation is different, and the compiler does not. This is why our teachers are so reluctant to tell us that the array name is the first address. So back to the question at the beginning, in our above words, a in & A can represent the entire array content, so the address is still the array address, of course, a can also be understood as an address at this time, but since a is a two-dimensional array, that is, a second-level pointer, & A should be a third-level pointer. So if you want to assign & A to a pointer variable, it should be like char
(* P) [2] [3]; * What does a represent? This problem is also very tangled... Is it the value of the first element of the array? You can try it. The result is still the address. Why? Next is the core of this article.

 

As we all know, the array name of a one-dimensional array is a level-1 pointer, while the name of a two-dimensional array is a level-2 pointer, and the name of a three-dimensional array is a level-3 pointer... and so on. I don't know how you understand level-2 and level-3 pointers. In my opinion, level-2 pointers are actually pointers, but they must point to a level-1 pointer in the space, the space pointed to by this level-1 pointer is the variable address that actually points. That is to say, the one-dimensional array name is a level-1 pointer, so for char
C [2] = {1, 2}; Because C is a level-1 pointer, & C should be a level-2 pointer. C can assign values to char * p type variables, while & C can only assign values to Char (* P) [2]; Type pointers. Note that P here is also a second-level pointer. In this case, you can use the above method to try how many bytes are different between the values of C + 1 and C? It should be big 2! * The value of C should be the value of the first element of the array. For a two-dimensional array char
C [2] [3] = {1, 2, 3}, {4, 5, 6}; C is a second-level pointer, you can also refer to the number of bytes difference between C + 1 and C? It should be 6! Since C is a second-level pointer, that is to say, * C is a first-level pointer, although the address pointing to is the same as the address pointing to C, the meaning of the expression is quite different at this time. C is a second-level pointer, and it points to a range from 1 ~ 6, while * C is a level-1 pointer, which ranges from 1 ~ 3. How much is the difference between * (C + 1) and * C? It should be 3 !. What about ** C? Obviously, the space pointed to by a level-1 pointer will not be the address, but should be the content in the address, that is, 1. The three-dimensional and four-dimensional situations are similar, and so on .....

 

Finally, let's take a look at the value of * (p + 1) + 2? First, P is a pointer. The address in it is a char [3] type address, and P is assigned a value, so p + 1 should point to the next char [3] type address, that is, to the first address of the second line of array A, * (p + 1) obviously, it is the content in the second row of exponential group A. According to the analysis just now, if it is an array, the content of an array is actually represented by its first address, so * (p + 1) indicates the first address of the second row of array A, and then * (p + 1)
+ 2 indicates the third address of the second line, and * (p + 1) + 2) indicates the content of the third address of the second line, that is, 6.

 

It seems that I have mentioned a lot about a very simple question. Next I want to ask a few questions to see if you have understood what I have said. Please refer to the following question:

1) for a one-dimensional array: Char C [3]; how can I define a pointer variable P0 so that p0 = C; can I compile it? How to define a pointer variable P1 so that P1 = & C can pass? * What does C mean?

2) for two-dimensional arrays: Char C [2] [3]; how to define a pointer variable P0 so that p0 = C; can be compiled? How to define the pointer variable P1 so that P1 = & C; * C represents? ** What does C mean? What are the differences and connections between these products?

 

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.