Easy to get started with C language pointers

Source: Internet
Author: User

& represents the address to be taken

* Indicates value

Constant pointer

pointer to a constant ,

int a = 4;
int B = 6;
int const *P = &a; Constant pointer
*p = 66; Cannot be modified, the value pointed to is a constant (cannot be changed by the pointer) (can only be read, immutable, function parameter chage (int const *p))
A = 44; The variable itself can be modified P = &b;
Pointer constants
A constant value when the pointer is over;
int C = 66;
int d = 99;
int *const pn = &c;
c = 100; can modify
PN = &d; Cannot be modified because the pointer pn is a constant type pointer
*PN = 1010; You can modify the value pointed to by the pointer
printf ("%d", c);
Pointer constants pointing to constants
------------------------------------------------------

Const (*) left, I am pointer variable point constant ;

Const (*) on the right side, I am the pointer constant pointing variable ;

Const (*) on both sides, I am the pointer constant pointing constant ;

Pointer variable can be changed to point, pointer constant cannot turn!

If all becomes constant, lock dead, I can't turn, you also don't want to change!

----------------------------------------------------------------

Pointer to multidimensional array

int map[4][3] = {
{A-i},
{4,5,6},
{7,8,9},
{17,18,19}
}
int (*p) [3] = map;

The P pointer can be viewed as such a form:

P[3] = map address {

MAP[0] address ,

MAP[1] address ,

MAP[2] Address

}

Map address: Save Map[0] Address, map[0] address is also take map[0][0] to save.

*map Address: Save the address of map[0][0], although the address is the same as the map but the size is different
P: Ibid.

*p: Ibid.

*MAP+1: The acquisition value is an address of map[0][1].

*p+1 : Ibid .

MAP+1: The acquisition value is an address of map[1]/ map[1][0]
P+1: Ibid.

* (map+1): First get map[1][0] address, then value, = 4

Same address
printf ("p:\t%p\n", p);
printf ("map:\t%p\n", map);
printf ("*map:\t%p\n", *map);
printf ("*p:\t%p\n", *p);
Same address
printf ("map+1:\t%p\n", map+1);
printf ("* (map+1): \t%p\n", * (map+1));
printf ("p+1:\t%p\n", p+1);
printf ("* (p+1): \t%p\n", * (p+1));
Same address
printf ("*p+1:\t%p\n", *p+1);
printf ("*map+1:\t%p\n", *map+1);



Detailed code

int map[4][3] = {
{A-i},
{4,5,6},
{14,15,16},
{7,8,9}
};
int (*p) [3] = map;

printf ("%d\n", * (*p+1));
printf ("%d\n", * (* (p+1)));
printf ("%d\n", * (* (map+1)));

printf ("p:\t%p\n", p);
printf ("map:\t%p\n", map);
printf ("*map:\t%p\n", *map);
printf ("*p:\t%p\n", *p);
printf ("map+1:\t%p\n", map+1);
printf ("* (map+1): \t%p\n", * (map+1));
printf ("p+1:\t%p\n", p+1);
printf ("* (p+1): \t%p\n", * (p+1));
printf ("*p+1:\t%p\n", *p+1);
printf ("*map+1:\t%p\n", *map+1);


This article from the "90 Design Studio" blog, reproduced please contact the author!

Easy to get started with C language pointers

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.