This week began to do C language pen test, what! The test is the concept, a variety of details, although it seems that the book can be found, but I really do not know ... Suddenly there is a kind of my C-language good slag feeling t_t
Well, next to the "on the computer Experiment Blue Book, comprehensive test question two" in the difficult 2 questions, these two questions make you to the "pointer array" and "array pointer" the difference is clearer.
"Example 1"
The following procedures are available:
1#include <stdio.h>2 3 intMain ()4 {5 Char*s[] = {" One"," Both","three"}, *p;6p = s[1];7printf"%c,%s\n", * (p+1), s[0]);8 return 0;9}
The result after execution is _______.
A. N, B. W, one C. T, one D. O, and
The first time I chose C, because I thought (p+1) is pointing to "three". This is an understanding error for the pointer p type.
The correct answer is B, because:
1#include <stdio.h>2 3 intMain ()4 {5 Char*s[] = {" One"," Both","three"};//S is an array of pointers, the element is 36 //pointer to a string constant7 Char*p = s[1];//p is a pointer variable that points to a string8printf"%c,%s\n", * (p+1), s[0]);9 //(p+1) is the address of P plus the size of a character memory, from point T to point WTen return 0; One}
So, usually we say that the pointer to the string, in fact, is pointing to a character, so it is a displacement operation, the addition and subtraction are 1.
Also, if you put the 8th line
printf ("%c,%s\n", * (p+1), s[0]);
Change into
printf ("%c,%s\n", * (p+1), s[0]);
The output will be: Wo,one
Because when we output a string, we actually pass the first address of the string to the printf () function, which is judged by the end of the ' \ n '.
"Example 2"
1#include <stdio.h>2 3 intMain ()4 {5 inta[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,Ten, One, A}};6 int(*p) [4] =A;7printf"%d\n", * (* (p+1)+3));8 return 0;9}
The result of the above code execution is _______.
The answer is 8.
I don't understand the problem at first. How can * also *, (*P) [4] What Ghost ... I messed up the "pointer array" and "array pointers" before I made this paper =
That is true:
1. (*p) [4]: declares that P is a pointer to (an array of 4 int elements), so p+1 from Point A[0] to a[1]
2.* (* (p+1) +3): Why are there two stars?
* (p+1) = a[1][0] (i.e. 5), the first star from a[1] to a[1][0], although the address has not changed, but the type of pointer changed! The original pointer +1 is a plus 4 int, now the pointer + 1 only add an int!!
* (p+1) +3 = A[1][3] (that is, 8), then take a * is to read from the address 8
You can also modify the original code to clarify:
1#include <stdio.h>2 3 intMain ()4 {5 inta[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,Ten, One, A}};6 int(*p) [4] =A; 7printf"P:%p\n", p);8printf"p+1:%p\n", p+1);9printf"* (p+1):%p\n", * (p+1));Tenprintf"* (p+1) + 3:%p\n", * (p+1)+3); Oneprintf"%d\n", * (* (p+1)+3)); A return 0; -}
The result of the output is:
0x7fff9f4ba180 P+10x7fff9f4ba190* (p+10x7fff9f4ba190* (p+1) +3 0x7fff9f4ba19c 8
Oh, yes.
"C language Written review" pointer arrays and arrays pointers