Android for JNI (iii)--C pointers, pointer variables, pointer common errors, value passing, reference passing, return multiple values

Source: Internet
Author: User

Android for JNI (iii)--C pointers, pointer variables, pointer common errors, value passing, reference passing, return multiple values

C in the more difficult this piece, is probably the pointer, so we still read a bit more information, of course, if just want to know, see this article is enough, but I also try to state very detailed

I. Pointers

To say the pointer, in fact, he should be a variable to save the memory address, let's look at a small example

#include <stdio.h>#include <stdlib.h>Main () {//int Variable       intI i =5;//Print the value of I       printf("%d\n", i);//Print memory address of I       printf("% #x \ n", &i);//Define a pointer variable      //The meaning of the pointer is that he is used to save the memory address       int* J; j= &i;//Print pointer       printf("% #x", j);//Let the window stay        intAge;scanf("%d", &age);}

It is not difficult to see from this paragraph that we define an I words can print values and memory address, but we define a pointer J, also can be assigned a pointer address, do not believe, we print

As you can see, the memory address is the same, so we can clearly understand the concept of pointers, the memory address pointed to the value of the village square is an int type

Since the pointer can receive a memory address, he can also convert it to a value, and we continue to see

//打印指针值printf("%d",*j

Outputs a pointer to a value of type int

It's 5, *p. Point to the value on the memory address

Binary pointers

The concept of a binary pointer, he is also a pointer variable, the address of the two-level pointer must be a first-level pointer, that is, J

        //定义一个二进指针       int ** k;        //二级指针存放的地址必须是一个一级指针,也就是j       k = &j;        //输出j的内存地址        printf("%#x\n",&j);        //打印二级指针‘       printf("%#x\n"

After we run this, K's memory address is the same as J's.

The relationship of pointers and pointer variables
    • The pointer is the address, the address is the pointer
    • The address is the number of the memory unit
    • A pointer variable is a variable that holds an address
    • Pointers and pointer variables are two different concepts
    • The pointer variable is said to be a pointer when we present it, but it doesn't have the same meaning.
Two. Common pointer errors

We sometimes run into some stems, which we need to be aware of, we define a pointer

        //定义一个指针,在内存中开辟         int * i;        //打印i的内存地址         printf("%#x\n"

This I assignment is not given, but can be printed

Here we know that the pointer is defined and there is a value, which is not an error in itself, but if you

    *i = 3; 

You have a mistake, because when the definition is random point to an address, you now assign the value of this address is 3, it is wrong, normal is the idea is

        //定义一个指针,在内存中开辟         int * i;        5;         //打印i的内存地址         printf("%#x\n",i);        *i3

So the output is 3.
So the conclusion we get is that before the pointer is copied, it's not a bucket *i assignment, and the type of the pointer can't be mixed.

Three, value passing and reference passing

This concept is also in Java, in order to use a lot of it, in fact, is to define an intermediate variable to convert, I write a small example of everyone is simple and clear

 #include <stdio.h>   #include <stdlib.h>  main () {/        /define two variables  int  i = 3 ;        int  j = 5 ;        //define intermediate variables    int  temp = i;     //temp = 3  i = j;  //i = 5  j = temp;        //j = 3         //let the window stay          int  age; scanf  ( "%d" , &age);}  

This will change the value, but it makes no sense, we use the function to simplify him, we first look at the concept of Java to pass the value of what is a type

#include <stdio.h>#include <stdlib.h>voidfunctionintIintj) {inttemp = i;            i = j;           j = temp; }main () {//define two variables        inti =3;intj =5;//Call functionfunction (I,J);printf(The value of "I is:%d\n", i);printf("J value is:%d\n", j);//Let the window stay        intAge;scanf("%d", &age);}

With the idea of Java, it should be written like this, let's run a bit

However, you will find that their values do not change, because in C, the value of the transfer itself does not affect the variable, in fact, in Java so there is no change in the case, left and right here is involved in a reference passed, we write a Java program to make an analogy

/** * Test class * @author LGL * */ Public  class Test {    //Define two global variables    inti =3;intj =5; Public Static void Main(string[] args) {Test test =NewTest ();        Function (test);        System.out.println (TEST.I);    System.out.println (TEST.J); }Private Static void Function(Test stest) {inttemp = STEST.I;        STEST.I = STEST.J;    STEST.J = temp; }}

With this idea, we can convert the values.

This is called the reference Pass, but then the process-oriented idea in C is no object, so the address can also be implemented, we try to

#include <stdio.h>#include <stdlib.h>void function (int* p,int*Q)     {inttemp =*p;*p=*q;*q= temp; }main () {//Define two variablesinti =3;intj =5;//Call function functions (&AMP;I,&AMP;J);printf("I has a value of:%d\ n", i);printf("J value is:%d\ n", j);//Let the window stayintAge; scanf"%d", &age);}

After we pass the memory address, we convert it, and then we can run the following

Concept of value passing and reference passing
    • Value passing: passing a normal value
    • Reference delivery: Passing a memory address

In fact, these should be called value passing, but the reference passed an address.

Four. Pointer return value

Why use pointers?

    • Pointers are direct access to the hardware
    • Fast data transfer
    • Returns more than one value
    • Represents a complex data structure
    • Easy handling of strings
    • Pointers help to understand object-oriented

If we were to write a return value in Java, it would be convenient to use return, but in C, she could return multiple values, let's write an example

#include <stdio.h>#include <stdlib.h>void function (int* p,int*Q)     {//To operate*p=*p * *;*q=*q  * *; }main () {//Define two variablesinti =3;intj =5;//Call function functions (&AMP;I,&AMP;J);printf("I has a value of:%d\ n", i);printf("J value is:%d\ n", j);//Let the window stayintAge; scanf"%d", &age);}

We run

You'll find that he's changed, and that's the concept that C can return multiple values, and he'll be able to manipulate it directly.

OK, this article idle here, came here, C has a vague impression, but has not touched the threshold, we have to continue to refuel is!

Android for JNI (iii)--C pointers, pointer variables, pointer common errors, value passing, reference passing, return 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.