Chinaunix首页 | 论坛 | 博客
  • 博客访问: 910894
  • 博文数量: 299
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2493
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-21 10:07
个人简介

Linux后台服务器编程。

文章分类

全部博文(299)

文章存档

2015年(2)

2014年(297)

分类: C/C++

2014-07-18 17:18:20

I found this symbols in a function declaration several times, but i don't know what they mean, if it is something stupid I apologize for the question.

like this:

That is taking the parameter by reference. So in the first case you are 
taking a pointer parameter by reference so whatever modification you do 
to the value of the pointer is reflected outside the function. Second is
 the simlilar to first one with the only difference being that it is a
 double pointer. See this example:


    void pass_by_value(int* p)
    {
    	//Allocate memory for int and store the address in p
    	p = new int;
    }
    
    void pass_by_reference(int*& p)
    {
    	p = new int;
    }
    
    int main()
    {
    	int* p1 = NULL;
    	int* p2 = NULL;
    
    	pass_by_value(p1); //p1 will still be NULL after this call
    	pass_by_reference(p2); //p2 's value is changed to point to the newly allocate memory
    
        return 0;
    }

share|edit
    
In actual code or a book? To me it just looks like notation for a general function. One which returns a single pointer, and the other a pointer to a pointer. –  Mr. Shickadance Apr 26 '11 at 11:48
    
Those are references to pointers. –  Alexandre C. Apr 26 '11 at 11:49
    
like this: void raccogli_dati(double **& V, double **p, int N) { int ultimo = 3; V = new double * [N/2]; for(int i=0; i < N/2; i++) { V[i] = new double[N/2], std :: clog << "digita " << N/2 - i << " valori per la parte superiore della matrice V: "; for(int j=i; j < N/2; j++) std :: cin >> V[i][j], p[ultimo++][0] = (V[i][j] /= sqrt(p[i][0]*p[j][0])); } for(int i=1; i < N/2; i++) for(int j=0; j < i; j++) V[i][j] = V[j][i]; } –  sdffadsf Apr 26 '11 at 11:49
    
yes, but what is the importance of using it in a function declaration, what are the difference with * and **, also with this syntax you don't copy the "values" –  sdffadsf Apr 26 '11 at 11:51
add comment

7 Answers

up vote15down voteaccepted

That is taking the parameter by reference. So in the first case you are taking a pointer parameter by reference so whatever modification you do to the value of the pointer is reflected outside the function. Second is the simlilar to first one with the only difference being that it is a double pointer. See this example:

That is taking the parameter by reference. So in the first case you are taking
 a pointer parameter by reference so whatever modification you do to the value 
of the pointer is reflected outside the function. Second is the simlilar to 
first one with the only difference being that it is a double pointer. See 
this example:


    void pass_by_value(int* p)
    {
    	//Allocate memory for int and store the address in p
    	p = new int;
    }
    
    void pass_by_reference(int*& p)
    {
    	p = new int;
    }
    
    int main()
    {
    	int* p1 = NULL;
    	int* p2 = NULL;
    
    	pass_by_value(p1); //p1 will still be NULL after this call
    	pass_by_reference(p2); //p2 's value is changed to point to the newly allocate memory
    
        return 0;
    }

share|edit

阅读(1144) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~