Pointer in C

Source: Internet
Author: User

The pointer stores the memory address, so when the code is executed

int *iptr;int a;iptr = &a;

It indicates that iptr points to the memory address of. If

*iptr = 10;printf("a = %d\n",a);

So what is the value of?

You can simply write a program cpoint1.c

#include<stdio.h>int main(){int a;int *iptr;iptr = &a;*iptr = 10;printf("a = %d\n",a);}

Compile with GCC on the terminal

#gcc cpoint1.c

Generate a. out execution file after execution

./a.out

The printed result is a = 10.

The pointer iptr points to the memory address of.

*iptr = 10;

Actually, 10 is saved to the memory address of.

Now that we understand the basic concepts of pointers, what are pointers?

First, let's assume that James, Xiaohua, and xiaoxin are friends, but James only has Xiaohua's phone number, while Xiaohua only has xiaoxin's phone number. If Tom wants to find Tom, he must first contact Tom and then contact Tom through Tom.

If Xiao Hua and Xiao Xin are both treated as memory addresses, Then Xiao Ming points to Xiao Hua, which can be understood as a level-1 pointer. Then Xiao Hua points to Xiao Xin, which becomes a pointer to the pointer.

int *iptr;int a;int **jptr;

We already know above.

iptr = &a;

It refers to assigning the memory address of a to iptr.

Then how should we assign a value to the jptr pointer. The example of James Xiaohua xiaoxin just now can be dissected in this way.

Int ** Xiao Ming; int * Xiao Hua; int Xiao Xin;

First, James needs to get Xiao Xin's address, so he needs to use Xiao Hua. So there are

Xiao Hua = & Xiao Xin;

In this way, Xiaohua points to xiaoxin, obtains the address of xiaoxin, and transfers the address of Xiaohua to Xiaoming, so that Xiao Ming can get the address of xiaoxin.

Xiao Ming = & Xiao Hua;

Next, let's look at the following program cpoint2.c.

#include<stdio.h>int main(){int a;int *iptr;int **jptr;iptr = &a;jptr = &iptr;a = 10;printf("jptr = %d\n",**jptr);}
#gcc cpoint2.c

The result of compilation and execution is:

jptr = 10

Do you understand?

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.