Let's talk about arrays and pointers first: Let's take a look at the following preparations ,,,
Int q [3] [4] = {1, 3, 5, 7}, {3, 4, 6}, {1, 6, 8, 9 }};
A two-dimensional array name, pointing to one-dimensional array a [0], that is, the address of zero rows
A [0], * (a + 0), * the address of column 0 in row 0 of
The first address of line a + 1, & a [1] 1
A [1], * (a + 1) Address of element a [1] [0] In column 0 IN a (same as a [1] + 0, * (a + 1) + 0)
A [1] + 2, * (a + 1) + 2, & a [1] [2] the address of zero-column element a [1] [2] in a row (same as above)
* (A [1] + 2), * (a + 1) + 2 ), value of a [1] [2] 1 row 2 leave element a [1] [2]
If you don't understand it, read it several times. Because the focus of my article is not here, I want to talk about sizeof () today, so you know! Haha
First of all, we should point out the mistakes that many beginners may make, that is, when they finish learning C or C ++, they always think that sizeof () is a function. In fact, sizeof () is not a function at all. It is a length operator. In fact, I always thought so at the beginning. Well, there is nothing to discuss about this problem!
First, let's talk about why sizeof () is not a function! In fact, there is nothing to do with it. He is not. Let's give an example to illustrate it!
# Include <stdio. h>
Void main ()
{
Int a, B;
B = sizeofa;
Printf ("% d \ n", B );
Getchar ();
};
Check the running result:
Imagine if it is a function, can the brackets of sizeof () be omitted? Obviously, this is not a function!
Now let's go to my topic. When we look at the length of sizeof (), there are several areas that are particularly prone to errors. Now, let's summarize them ,, the best example is the code. Let's look at the following code:
Running result:
# Include <stdio. h>
Void main ()
{
Int int_data = 0;
Float float_data = (float) 1.2;
Double double_data = (double) 4.3;
Int * int_ptr_1 = & int_data;
Char ch = 'a ';
Char * char_ptr_2 = & ch;
Char char_1 [5] = {'A', 'B', 'C', 'D', 'E '};
Char char_2 [4] [5] = {
{'A', 'B', 'C', 'D', 'E '},
{'A', 'B', 'C', 'D', 'E '},
{'A', 'B', 'C', 'D', 'E '},
{'A', 'B', 'C', 'D', 'E '},
};
Printf ("sizeof (int_data) % d \ n", sizeof (int_data ));
Printf ("sizof (float_data) % d \ n", sizeof (float_data ));
Printf ("sizeof (double_data) % d \ n", sizeof (double_data ));
Printf ("sizeof (int_ptr_1) % d \ n", sizeof (int_ptr_1 ));
Printf ("sizeof (char_ptr_2) % d \ n", sizeof (char_ptr_2); // the pointer variable will assign a machine byte for storing the address value
Printf ("sizeof (char_1) % d \ n", sizeof (char_1 ));
Printf ("sizeof (char_2) % d \ n", sizeof (char_2 ));
Printf ("sizeof (char_2 [1]) % d \ n", sizeof (char_2 [1]);
Printf ("sizeof (* (char_2 + 1) % d \ n", sizeof (* (char_2 + 1 )));
Printf ("sizeof (char_2 + 1) % d \ n", sizeof (char_2 + 1 ));
Printf ("sizeof (& char_2 [1]) % d \ n", sizeof (& char_2 [1]);
Printf ("sizeof (char_2 [1] [2]) % d \ n", sizeof (char_2 [1] [2]);
}
The running result is as follows:
A little explanation of the above situation!
1, sizeof (char_ptr_2); // the pointer variable is allocated with a machine Character length (generally four bytes, different digits) used to store the address value, although char_ptr_2 only points to a char type.
2. In sizeof (char_1) and sizeof (char_2), both char_1 and char_2 are array names. They represent the entire array, although the address is char_2, it represents the address of zero rows, but it represents the two-dimensional array char_2 [4] [5]; so ,,,,,
Char_1 points to the first address of an array char_1 [5], but it is an array name representing the whole of an array. Note that, unlike the sizeof (chr_1_ptr) and sizeof (char_2_ptr), the pointer variables char_1_ptr and char_2_ptr have an address, that is, a machine word length!
3. The address of char_2 [1] is the address of char_2 [1] [0], which is actually the first address of the second row of the two-digit array, in fact, the second row of a two-dimensional array is a one-dimensional array, while cahr_2 [1] has the same meaning as char_1 (playing the same role ). It is the first address of a one-dimensional array, so the values of sizeof (char_1) and sizeof (char_2 [1]) are the same.
4. What is different from 3 is that it is not the case of sizeof (char_2 + 1). You can see that cahr_2 + 1 is the address of the second line, at this time, his value is also an address, that is, a machine font!
Pay special attention to the subtle differences between 3 and 4. When a pointer represents an address and an array name (that is, when representing an array, whether the book is one-dimensional or multi-dimensional ), the results calculated by sizeof () are not the same.
In other words, the return address of the function, whether it is the array name or the pointer variable as the return value of the function, should be the same for the sizeof () calculation results!
For example:
View plainc
View plain # include <stdio. h>
Char * place_1 ();
Char * place_2 (char *);
Void main ()
{
<Span style = "white-space: pre"> </span> char a [5] = {'A', 'B', 'C '};
<Span style = "white-space: pre"> </span> char * p =;
<Span style = "white-space: pre"> </span> printf ("sizeof (a) % d \ n", sizeof ());
<Span style = "white-space: pre"> </span> printf ("sizeof (p) % d \ n", sizeof (p ));
<Span style = "white-space: pre"> </span> printf ("sizeof (place_1 () % d \ n ", sizeof (place_1 ()));
<Span style = "white-space: pre"> </span> printf ("sizeof (place_2 (p) % d \ n ", sizeof (place_2 (p )));
}
Char * place_1 ()
{
<Span style = "white-space: pre"> </span> char a [5] = {'A', 'B', 'C '};
<Span style = "white-space: pre"> </span> printf ("sizeof (a) % d \ n", sizeof ());
<Span style = "white-space: pre"> </span> return;
}
Char * place_2 (char * p)
{
<Span style = "white-space: pre"> </span> printf ("sizeof (p) % d \ n", sizeof (p ));
<Span style = "white-space: pre"> </span> return p;
}
The running result is as follows:
Bibliography: C Programming (Third edition), C language deep analysis