Android JNI Programming (iii) Preliminary understanding of--c language pointers, pointer variables, interchange two numbers, function returns multiple values

Source: Internet
Author: User

Copyright notice: This article from Administration Blog, reproduced please indicate the source: http://blog.csdn.net/a_zhon/.

Directory (?) [+]

I. What is a pointer?

Simply put: The pointer is the memory address of the memory address is the pointer. Let's see a little case.

#include<stdio.h>#include<stdlib.h>/**  指针就是内存地址  内存地址就是指针 */main() {    //定义一个int类型的变量i,并且赋值为10    int i=10;    //定义一个int类型的一级指针变量p int* p; //把i对应的地址给p变量 p=&i; //指针取值*p:把p变量地址对应的值取出来 printf("*p=%d\n",*p); //打印地址值 printf("p的地址值%#x\n",p); system("pause");}

Summary: P holds an address value, and *p is the value that corresponds to the address value. Two. Following four small cases to deepen the understanding of pointers and pointer variables, let's first talk about the difference between pointers and pointer variables:
    1. The pointer is the memory address, the memory address is the pointer
    2. The memory address is the number of the memory unit
    3. The pointer variable is the variable that holds the memory address
    4. Pointers and pointer variables are two completely different concepts
1. The first example, if I change the value, p-value changes?
#include<stdio.h>#include<stdlib.h>main() {    int i=100;    //p就是我们的指针变量,存放地址值 int *p=&i; printf("i值修改之前,p的地址值是%#x\n",p); i=200; printf("i值修改之后,p的地址值是%#x\n",p);}

The answer is no change:because the I value is modified at this time, p refers to the address of I, so P will not change2. The second example, if I change the value of P, I value changes?
#include<stdio.h>#include<stdlib.h>main() {    int i=100;    int *p=&i; printf("p值修改之前,i的值是%d\n",i); int j=200; p=&j; printf("p值修改之后,i的值是%d\n",i);}

The answer is no change:because the address of J is given p, and I does not move, so the value of I does not change .3. The third example, if I change the value of the *p value changes?
#include<stdio.h>#include<stdlib.h>main() {    int i=100;    int *p=&i; printf("i值修改之前,*p的值是%d\n",*p); i=200; printf("i值修改之后,*p的值是%d\n",*p);}

The answer is a change:because the I value is modified at this time, *p refers to the value that the address of I points to, so the value of *p has changed4. The fourth example, if I change the value of the *p, I value changes?
#include<stdio.h>#include<stdlib.h>main() {    int i=100;    int *p=&i; printf("*p值修改之前,i的值是%d\n",i); *p= 200; printf("*p值修改之后,i的值是%d\n",i);}

The answer is a change:Because of the *p=200, the value of the *p reference is modified to 200, that is, the value of I is modified. Three: three meanings of "*" number
    1. Math Operator: 3 * 7 = 21
    2. Define pointer variable: int* p;
    3. Pointer operator (value): *p represents the value of p corresponding to the memory address
Four: Operation memory address implementation of the two-digit interchange

1. The first: the use of intermediate variables to exchange, this is one of our most common method

#include<stdio.h>#include<stdlib.h>/**    互换两个数 */main() {    int a = 100;    int b = 150; //通过一个中间变量来互换 int temp = a; a = b; b = temp; printf("a的值:%d\n",a); printf("b的值:%d\n",b); system("pause");}

2. The second kind: we know that C language can directly manipulate memory, so we can directly change the value of the memory address of a, b two.

 #include <stdio.h> #include <stdlib.h> /** interchange two number *///Interchange digital int* a,int* b) { //temp takes the value of the memory address of the pointer variable A, which is int temp = *a; //Change the value of a corresponding memory address to the value of B corresponding memory address *a = *b; //Modify the value of B corresponding memory address to temp *b = temp;} Main () {int a = 100; int B = 150; //will take the address of A, B, changeadress (&a,&b); printf ( "a" value:%d\n " , a); printf (" pause ");}     

Five: function returns multiple values
#include<stdio.h>#include<stdlib.h>/**    函数返回多个值    通过修改指针变量引用的值,达到返回多个值的效果。 */void close(int* a,int* b) {    *a=0; *b=0;}main() { int a=1; int b=1; //调用函数 close(&a,&b); printf("a=%d\n",a); printf("b=%d\n",b); //是docs窗口停留 system("pause");}
It is not difficult to sum up, the interchange is a original point to 100 to point to 150, the B turns to 150 to point to 100; that is, the address of a B has not changed, it is their value; Okay, here we go. The preliminary understanding of pointers and pointer variables is finished.

Android JNI Programming (iii) Preliminary understanding of--c language pointers, pointer variables, interchange two numbers, function returns multiple values

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.