C pointer parsing ------ operator & amp; and *

Source: Internet
Author: User

C pointer parsing ------ operators & amp; and *
This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020


& Is an address operator. * is a pointer or indirect operator. & The operation result of a is a pointer. the pointer type is a type plus *. The pointer type is a type, the pointer value, that is, the memory zone pointed to by the pointer, is the address of. * P has more computation results. In short, * p is what p points to. It has these characteristics: its type is the type pointed to by p, the address it occupies is the address pointed to by p.

See the following example:

Int a = 5;

Int B;

Int * p;

Int ** q;

P = & a; // The result of & a is a pointer of the int type *, int type, and a type.

* P = 10; // * p result. Here its type is int, and the address it occupies is the address pointed to by p, that is, the address of. Obviously, * p is variable a. The essence of this Code is to assign a value to.

Q = & p; // The result of & p is a pointer. The type is p type plus *. Here it is int **. The Pointer Points to the p type. Here it is int *. The Pointer Points to the p address.

* Q = & B; // * q is a pointer, and the result of & B is a pointer. The types of the two pointers are the same as those of the pointer, so this code is to assign a value to * q.

** Q = 34; // * the q result is what q points to. Here it is a pointer, that is, int *, and another operation is performed on this pointer, the result is an int-type variable. The address it occupies is the address pointed to by * q, that is, the address of B. Therefore, the purpose of this sentence is to assign a value to variable B.

Finally, you can verify the following:

#include 
 
  int main(){int a = 5;    int b;    int *p;    int **q;    printf("a = %d\n",a);    p = &a;            *p = 10;    printf("a = %d\n",a);           q = &p;           *q = &b;             **q = 34;     printf("b = %d\n",b);}
 
Result:

a = 5a = 10b = 34

To understand these two operators, you must grasp the types of the results of the two operations and the types pointed.



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.