指针数组和函数 [英] pointers arrays and functions

查看:75
本文介绍了指针数组和函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题需要一些我没有的理论.
首先,数组基本上是指向数组定义中第一个元素的指针,并且根据指定的索引,它可以依次引用所有数据.正确的?

This question requires a bit of theory that i don''t have.
First off, an array is basically the pointer to the first element in the array definition, and depending on the index specified, it can reference all of the data sequentially after it. Correct?

int array[5];

这就是为什么我们可以只声明一个指针,然后像下面这样为它分配值数组的原因:

Which is why we can just declare a pointer, and then allocate an array of values to it afterwards like so:

int * array;
array = new int[5];

现在,为什么当我将int指针传递给函数时,为什么要在其中分配空间,然后在函数运行后尝试访问这些值,我得到访问冲突错误?

这是一个示例:

So now, why is it that when I pass an int pointer like to a function, allocate the space there and then after the function has run try to access those values, I get an access violation error?

Here''s an example:

#include <iostream>

void function( int * Array)
{
     Array = new int[5];
     for(int i = 0; i < 5; i++)
         Array[i] = 0;           // works fine here
}

int main(int argc, char * argv[])
{
     int * Array = NULL;
     function(Array);
     Array[3] = 2;               // access violation here
     system("PAUSE");
     return 0;

} 

推荐答案

那是因为指针例如int*本质上是一个整数(在内存中的位置),当您将此指针传递给函数时,您传递的是指针的整数而不是指针的地址.
因此,要更改function中指针的值,您必须传递指针的地址而不是它看起来像这样的值:

That''s because a pointer e.g. int* is essentially an integer (a location in memory) when you pass this pointer to your function you pass the integer of the pointer and not the address of the pointer.
So inorder to change the value of a pointer in your function you''ll have to pass the address of the pointer and not the value it would look like this:

#include <iostream>
void function(int ** Array)
{
     *Array = new int[5];
     for(int i = 0; i < 5; i++)
         Array[i] = 0;           
}


int main(int argc, char * argv[])
{
     int * Array = NULL;
     function(&Array);
     Array[3] = 2;               
     system("PAUSE");
     return 0;
}



我很难解释这一点,因此,与其坐在这里重写5次,不如让我问您对我的解释或发布的更新后的源代码有任何疑问,我将尽力去做更详细.


刚刚找到本文指向指针的指针和指向指针的引用 [



I find it hard to explain this, so instead of sitting here and rewrite this 5 time, I''ll just let you ask any questions you have regarding my explanation or the updated source code I posted, and I''ll try to go into more detail.


Just found this article Pointer to Pointer and Reference to Pointer[^] I haven''t read it myself but it seems to go over double pointers in more detail. In any case it doesn''t hurt to have multiple sources of information :)


让我们以稍微不同的样式编写代码:
Lets write your code with a little bit different style:
#include <iostream>

void function( int* Array)
{
     Array = new int[5];
     for(int i = 0; i < 5; i++)
         Array[i] = 0;           // works fine here
}
 

int main(int argc, char* argv[])
{
     int* Array = NULL;
     function(Array);
     Array[3] = 2;               // access violation here
     system("PAUSE");
     return 0;
 
}


(只需将*放在int附近,这样很明显int*是类型名称)
现在将其与这一比较:


(just put the * close to int, so that it is clear that int* is a type name)
and now compare it to this one:

#include <iostream>

void function( int val)
{
   val = 5;
}
 

int main(int argc, char * argv[])
{
     int val =0;
     function(val);
     std::cout << val << std::endl;
     system("PAUSE");
     return 0;
 
}



您对结果显示为"0"而不是5感到惊讶吗?

现在,您的function,int *数组是指向int的指针"类型的值,-on函数调用-指向与主Array相同的位置(在这种情况下为NULL),但是在函数内部,您更改了指针指向其他内容(在您的情况下为new int[5]),而不是将值分配给指针数组.
但是,在函数内部,没有任何内容可以使main 所做的更改立即生效:Array,主要仍然指向NULL,并且您尝试编写int NULL+2.

要解决该问题,您应该在main中(而不是调用function)并在函数内部进行分配,请使用指针将值分配给元素.
或者,将引用传递给指针(这样,如果您更改它,原始"也将被更改:



Are you surprised in seeing "0" as a result, instead of 5?

Now, your function, int* array is a value of type "pointer to int", that -on function call- points to the same location as the main Array (NULL in this case) but, inside function you changed the pointer to point to something else (new int[5], in your case), than you assign values to the pointed array.
But inside your function there is nothing that makes main to now about the change you did: Array, in main is still pointing to NULL, and you try to write int NULL+2.

To solve the problem you should allocate in main, than call function, and inside the function, use the pointer to assign the values to the elements.
Or, pass a reference to the pointer (so that if you change it, the "original" is also changed:

#include <iostream>

void function( int*& Array) //note the &
{
     Array = new int[5];
     for(int i = 0; i < 5; i++)
         Array[i] = 0;           
}
 

int main(int argc, char * argv[])
{
     int * Array = NULL;
     function(Array);
     Array[3] = 2;               
     system("PAUSE");
     delete[]Array;      //required, or will result in a memory leak.        
     return 0;
 
}


这篇关于指针数组和函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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