Pointer assignment int A [5] = {.... ...}, Differences between a, a [0], and a: C/C ++ mandatory candidates for job interviews (V ).

Source: Internet
Author: User

First, let's look at the Code:

[HTML]
View plaincopyprint?
  1. <Span style = "font-size: 18px"> # include <stdio. h>
  2. Void main ()
  3. {
  4. Char A [] = "brucelee! ";
  5. Char * P =;
  6. Printf ("% C \ n", * (p + 4 ));
  7. Printf ("% C \ n", P [4]);
  8. Printf ("% s \ n", P );
  9. Printf ("% C \ n", a [4]);
  10. Printf ("% C \ n", * (a + 4 ));
  11. Printf ("% s \ n", );
  12. } </Span>
#include <stdio.h>void main(){char a[] = "BruceLee!";char *p = a;printf("%c\n", *(p+4));printf("%c\n", p[4]);printf("%s\n", p);printf("%c\n", a[4]);printf("%c\n", *(a+4));printf("%s\n", a);}


First, the program declares the character array A and initializes it. Remember that '\ 0' is followed by default. Then the character pointer is declared, pointing to the first address of array A, which is also the address of a [0]. Here

[HTML]
View plaincopyprint?
  1. Char * P = A; equivalent to char * P = & A [0]
Char * P = A; equivalent to char * P = & A [0]

. * (P + 4) = P [4] = A [4] = * (a + 4). This is the final result. Note that the character pointer P and array name a each have two reference methods, one is the array reference method, such as a [4], one is the * (a + 4) pointer method.

Note that

[HTML]
View plaincopyprint?
  1. <Span style = "font-size: 18px"> printf ("% s \ n", P); the printed content is still <span style = "font-size: 18px "> </span> <PRE class = html name =" code "> brucelee! </PRE> <PRE class = html name = "code"> </PRE>
Printf ("% s \ n", P); the printed content is still[HTML]View plaincopyprint?
 
 
  1. Brucelee!
BruceLee!
[HTML]
View plaincopyprint?

That is to say, we only use * (p + 4) to view the content, but it does not change the point of P. P still refers to the first address, so the printed string starts with the first character.

If we add the following two sentences to the Code:

Printf ("% C \ n", * P ++ );
Printf ("% s \ n", P );

The first execution result is to print the first character 'B', and then P moves back one character. Therefore, the following print string starts with the second character and returns rucelee.

CONCLUSION: (* P + 4) This method does not change the point of pointer p. Only when P ++, p --, or P + = 4 is similar can the pointer P be changed!

Here is an example:

[CPP]
View plaincopyprint?
  1. <Span style = "font-size: 18px"> # include <stdio. h>
  2. Void main ()
  3. {
  4. Int A [5] = {1, 2, 3, 4, 5 };
  5. Int * P =;
  6. Printf ("% d \ n", a [0]);
  7. Printf ("% d \ n", * P );
  8. Printf ("% d \ n", P );
  9. Int * q = (int *);
  10. Printf ("% d \ n", Q [0]);
  11. } </Span>
#include <stdio.h>void main(){    int a[5] = {1, 2 ,3 , 4, 5};    int *p = a;    printf("%d\n", a[0]);   printf("%d\n", *p);   printf("%d\n", p);   int *q = (int *)a;   printf("%d\n", q[0]);   }

There are two points to be mentioned:

1,

[CPP]
View plaincopyprint?
  1. <Span style = "font-size: 18px"> printf ("% d \ n", P); </span>
printf("%d\n", p);

This statement cannot be executed normally, and no numbers related to array a are printed.Do not expect a head address like printf ("% s", P) p. This method is used to print the content of the entire array!

2, int * P = A; and int * q = (int *) A. The two statements have the same effect. The preceding mandatory conversions are the same.

Let's look at this confusing example:

[CPP]
View plaincopyprint?
  1. # Include <stdio. h>
  2. Void main ()
  3. {
  4. Int A [5] = {1, 2, 3, 4, 5 };
  5. Int * m = (int *) & A [0];
  6. Printf ("% d \ n", * (m + 1 ));
  7. Int * P = (int *) &;
  8. Printf ("% d \ n", * (p + 1 ));
  9. Int * q = (int *) (& A + 1 );
  10. Printf ("% d \ n", * (q-1 ));
  11. Int * w = (int *) (& A [0] + 1 );
  12. Printf ("% d \ n", * (W-1 ));
  13. }
#include <stdio.h>void main(){    int a[5] = {1, 2 ,3 , 4, 5};     int *m = (int *)&a[0];     printf("%d\n", *(m + 1));    int *p = (int *)&a;    printf("%d\n", *(p + 1));    int *q = (int *)(&a + 1);    printf("%d\n", *(q-1));    int *w = (int *)(&a[0] + 1);     printf("%d\n", *(w-1));    }

There are three key points:

1. Note that (int *) is dispensable, but whenAfter & is added, in int * m = (int *) & A [0], if the conversion is not enhanced, an alarm is triggered. Because the address is obtained here, it is best to force conversion!

2, (int *) (& A [0]) and (int *) (& A), in terms of printing m and P values, it seems that there is no difference between the two operations, but this is not the case! & A [0] = A indicates the first address of array A, that is, the address of a [0. But & A is the whole object, that is, the whole first address of the array. Which of the following is the address of the pointer Q?

We must remember to add 1 to the pointer to get the address of the next element, rather than directly adding 1 to the value of the original address. We certainly know this. If the type is X, after 1 is added, the pointer moves sizeof (x) backward, and the movement is in the unit of sizeof (X!

The more I say it, the less I understand it. It is actually the difference between a and a [0] And &!

The first two are equivalent. & A as mentioned above, regards A as a whole, so & A + 1 is the address of the next object of A, that is, & A + 1, relative to a or & A [0], the sizeof (A) = 5*4 = 20 bytes are moved. Here, the pointer Q points to a [5]! Therefore, the value of * (Q-1) is 5, that is, the value of a [4. In order to compare the differences, I finally took (& A [0] +
1) For comparison, W points to a [1] in the declaration, * (q-1) value is 1, that is, a [0] value.

3,Int * n = A is equivalent to int * n = (int *) & A [0]. int * n = (int *) &;

But when there is a pointer addition or subtraction operation, the results of the two are never the same!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!

For example, int * n = (int *) (& A [0] + 1) is never equal to int * n = (int *) (& A + 1 ).

 

Http://blog.csdn.net/yanzi1225627/article/details/7858231

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.