Home >Java >javaTutorial >Introduction to C++ references and pointers

Introduction to C++ references and pointers

坏嘻嘻
坏嘻嘻Original
2018-09-14 17:06:071799browse

Like high-level languages ​​such as Python and JavaScript, PHP can also write extended functions through C/C. Here we share how to build a simple PHP extension and how to call a third-party DLL library.

  1. c language pointers
    Pointers have abused me thousands of times. I fell in love with pointers. I was very confused when I first started learning pointers. Now it seems that pointer knowledge serves as a The role of the middleman, from the perspective of memory, is the address of the pointer space used to store data.

#include<stdio.h>void  add_1(int num)
{
    num++;
}

void add_2(int *num)
{
    (*num)++;
}int main()
{    int number1 = 1,number2 = 1 ; 
    add_1(number1);
    add_2(&number2);    printf("%d\n",number1);    printf("%d\n",number2);    return 0;
}


Introduction to C++ references and pointers

The value of number1 remains unchanged, number2 increases by 1, and the parameters are passed in add_1() It is equivalent to opening up a space and copying the value of number1 to the space . All operations performed on the copied value will have no impact on the original data. The parameters passed in add_2() are to open up a space to store the address of number2. All operations on num are equal to operations on number2.

  1. c Quote
    The code below is almost the same as the code above. Only a few symbols have been modified, but the effect is the same.

#include<iostream>using namespace std;void add_1(int num)
{
    num++;
}void add_2(int &num)
{
    num++;
}int main()
{    int number1 = 0, number2 = 0;
    add_1(number1);
    add_2(number2);    cout << number1 << "  " << number2 << endl;    return 0;
}

Introduction to C++ references and pointersc The reference is that gives number2 an alias . The memory address of number2 is actually stored in the memory, but the compiler has made some optimizations. And limitations, as I understand computer science, are that different ways of organizing things lead to different computer architectures.

3. c reference as return value

#include<iostream>using namespace std;int n = 3;int& func()
{    return n;
}int main()
{    int num;
    num = func();    cout<<num <<endl;    return 0;
}

The function returns an implicit pointer to n, but the object that needs to be referenced must not exceed the scope, that is to say, it depends on whether the variable acts on Within the function body, global variables like the above can be referenced, but not if placed within the func function.

4. c Some differences between references and pointers

  • Pointers can be null pointers, but references must point to a legal address space.

  • A pointer can point from one object to another object, but once the reference is initialized, the object cannot be changed.

  • Pointers can be initialized at any time, but references must be initialized when they are created.

Related recommendations:

Write PHP Extension using C/C

Look at C through static local variables, Features of C, C#, Java, PHP


The above is the detailed content of Introduction to C++ references and pointers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn