在函数中传递双指针,但不更改main :( [英] Passing double pointer in function but it don't change it's values in main :(

查看:86
本文介绍了在函数中传递双指针,但不更改main :(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以指导我我的代码有什么问题吗

Can any body guide me what is wrong with my code

void main()
{
	
	double **data;
	data=new double*[1];
	data[0]=new double[3];
	int rows=reading(2,data);
	for(int i=0; i<rows; i++)
	{
		for(int j=0; j<3; j++)
		{
			cout<<data[i][j]<<"\t";
		}
		cout<<"\n";
	}
	cout << rows<<endl;
	cout<<data[0][2]<<"\n";
}
int reading(int r,double **data)
{
	if (r%2!=0)
	{
		r++;
	}
	int rows=r;
	delete[] data;
	data=new double*[rows];
	for(int k=0; k<rows; k++)
	{
		data[k]=new double[3];
	}
	for(int i=0; i<rows; i++)
	{
		for(int j=0; j<3; j++)
		{
			data[i][j]=i+j;
		}
	}
	return rows;
}



我想做的是使用一个函数将内存分配给2D动态数组,但是函数在函数中被调用时一切正常,但是当它返回行并返回到main时,**数据仍然是其初始地址,而不是通过分配分配的地址新的存储空间功能.当我在函数中将数据作为双指针传递时,在函数中进行更改时应该将其更改.



I want to do is to allocate memory to 2D dynamic array using a function, but function is called in function everything works fine, but when it return rows and back to main, the **data remain its initial address instead of address allocated by assigning new memory space in function. As i passed data as a double pointer in function it should get changed when changes are made in function.

推荐答案

传递值(或它们的指针)放在堆栈,
因此它们仅在接收函数中可见" :):
The passing values (or their pointers) are placed on the stack,
so they are "visible" in the receiving functions only :) :
void f(int iPar)
{
  iPar++; // the local only increment
}

void d(int* piPra)
{
  if (piPar) {
    (*piPar)++; // outgoing increment
  }
}

void test()
{
  int i(2);
  f(i);
  // i == 2
  d(&i);
  // i == 3;
}



操作newdelete还将修改传递的指针:
-仅本地,当指针作为值传递时
-传出,当指针通过其地址传递时(指向指针的指针)

现在您可能会了解您的需求? :)

当然,它将是一个修改后的函数:



The operations new or delete will also modify the passed pointers:
- local only, when the pointers passed as value
- outgoing, when the pointers passed by their addresses (pointer to pointer)

Now you will probably understand what you need ? :)

Of course, it will be a modified function:

int reading(int iRows, double*** pppData)
{
...
}


如果要在函数中更改** data,则应将***data传递给它,在主函数中使用&data.(对不起,我的英语不是很好)
if you want to change ** data in function , you should pass a ***data to it , use &data in main function.(Sorry ,my English is not very good )


顶级值按值传递给函数.那是值被复制.

将此视为基本指针的崩溃过程,并按值传递.
您应该真正阅读一些相关内容,尽管我不推荐一本好书,因为我只是通过玩耍来了解发生了什么.

考虑一个更简单的示例:
The top level value is passed into a function by value. That is the value is copied.

Consider this a crash course in basic pointers and pass by value.
You should really read something about it, although I couldn''t recommend a good book as I just learnt things by playing around to see what happened.

Consider a more simple example:
char *szStr = "hello";


变量szStr现在包含一个指向内存的值,其中< code>"hello"</code>已存储.
对于此示例,我们将说此存储位置为< code> 0x1000</code>. 这意味着在存储器位置< code> 0x1000</code> -< code> 0x1006</code>我们有字符"h","e","l","l","o","\ 0"



The variable szStr now contains a value that points to the memory where <code>"hello"</code> is stored.
For this example, we will say that this memory location is <code>0x1000</code>.
This means that at memory location <code>0x1000</code> - <code>0x1006</code> we have the characters ''h'', ''e'', ''l'', ''l'', ''o'', ''\0''


//Now call a function passing in the pointer szStr
PrintStr(szStr);

void PrintStr(char *szPrint) {
	++szPrint; //Increase the value of the pointer, so that we start printing from the 2nd char
	//This does not change the value of szStr, which was what was passed into the function
	//szPrint is now 0x1001, which points to "ello"
	//szStr is still 0x1000, which points to "hello"
	puts(szPrint);
	szPrint[0] = ''E'';
	//szPrint is now 0x1001, which points to "Ello"
	//szStr is still 0x1000, which points to "hEllo"
	//we can change the data been pointed to just fine in both copies
}


实际发生的是我们将指针szStr的值(0x1000)复制到函数PrintStr的堆栈中.
因为我们已经传递了副本,所以对指针值的此副本所做的任何更改都不会更改为szStr的值.
但是,szStr和副本(szPrint参数)都包含0x1000,因此引用了相同的内存位置.
这意味着对szPrint中的字符所做的任何更改也将在szStr
中进行更改
因此,回到我的第一句话,这里的顶层是存储在szStrszPrint(0x1000)中的内存位置.
不符合您的实际问题.
如先前所建议,您可以制作一个三重指针并将其传递给:


What is actually happening here is that we are copying the value of the pointer szStr (which is 0x1000) onto the stack for the function PrintStr.
Because we have passed in a copy, any changes made to this copy of the pointer value wont be made to the value of szStr.
However, both szStr and the copy (szPrint parameter) contain 0x1000, and hence reference the same memory location.
This means that any changes made to the characters in szPrint will also be changed in szStr

So, referring back to my first sentence, the top level here is the memory location that is stored in szStr and szPrint (0x1000).

No to your actual problem.
As has been previously suggested, you can make a tripple pointer and pass that in:

void main(){
	double **data;
	//initialise as normal
	int rows=reading(2,&data);
	//rest as normal
}
int reading(int r,double ***data) {
	if (r%2!=0) {
		r++;
	}
	int rows=r;
	delete[] *data;
	*data=new double*[rows];
	for(int k=0; k<rows; k++) {
		(*data)[k]=new double[3];
	}
	for(int i=0; i<rows; i++) {
		for(int j=0; j<3; j++) {
			(*data)[i][j]=i+j;
		}
	}
	return rows;
}



或者,由于您已表明您正在使用C ++,因此可以使用按引用传递,它使用一些巧妙的技巧在将值传递给函数时不复制该值,也意味着您不必担心做诸如

看起来像:



Or, since you indicated you are using C++, you can use pass by reference, which uses some clever tricks to not copy the value when passing it into the function, and also means that you dont need to worry about doing crazy things like (*data)[i][j]=

This would look like:

void main() {
	//This code is unchanged. Convenience is nice.
}
int reading(int r,double &**data) { //Notice the &
	//This code is unchanged too.
	//Much simpler and prettier.
}


这篇关于在函数中传递双指针,但不更改main :(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆