General pointers and pointers to pointers

Source: Internet
Author: User

1. Pointer to integral type

#include <stdio.h>
int main ()
{
int a=5;
int *p;
p=&a;
printf ("a=%d &a=%x *p=%d p=%x \ n", a,&a,*p,p);
printf ("Pointer variable P's address =%x\n", &p);

}

A &a *p P
5 12ff7c 5 12ff7c
&p
12ff78

2. Pointer to character

#include <stdio.h>
int main ()
{
Char str[5]= "ABCD";
Char *p;
P=STR;
printf ("str=%s p=%s str=%x *p=%c p=%x \ n", str, p, str, *p, p);
printf ("Pointer variable P's address =%x\n", &p);

}

Why is the integer pointer p output the address, and the character pointer output is a string, the character pointer is not stored in the address?

Here we explain: for the pointer you use the%s output is a string, with%x is the address

3 . Pointer to an integral type pointer

#include <stdio.h>
int main ()
{
int a[6]={1,2,3,4,5};
int *p=a;
int * (*PTR) =&p; Because it is a pointer, so the address is stored, so to add &

printf ("a=%x\n", a); That is, the first address of the array 12ff68

printf ("p=%x\n", p); The value stored by the P pointer variable is the first address of the array 12ff68

printf ("&p=%x\n", &p); The address of the P pointer variable itself is 12FF64

printf ("ptr=%x\n", PTR); The PTR pointer variable stores the address of the P pointer variable 12ff64

printf ("&ptr=%x\n", &ptr); The address of the PTR pointer itself 12ff60

printf ("*ptr=%x\n", *ptr); *ptr refers to the 12ff68 address.

printf ("**ptr=%d\n", **ptr); **ptr =1

}

General pointers and pointers to 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.