【问题标题】:Having difficulty working with pointers难以使用指针
【发布时间】:2014-01-23 00:00:29
【问题描述】:

我在初始化指针时遇到了一些问题。

void findMM (int *PMM, int *theG)
{
        // code  I haven't written yet. It will essentially take two variables from   //theG and store it in MM  
}

int main() 
{ 
    int size;
    int MM [2] = {1000, 0}; 
    int *theG = NULL; 
    cout << "\nPlease insert size of array:" << endl; 
    cin >> size; 
    theG = new int [size];  
    findMM(&MM, &theG); //Get error with &MM  
    delete [] theG;     
    return 0; 
}

编译器说int (*)[2] 类型的参数与int ** 类型的参数不兼容所以很明显我的代码有问题,特别是我的(参考?)数组MM。或者也许还有其他我遗漏的明显错误?

编辑尝试 2

void findMM (int *PMM, int *theG)
{
        PMM [1] = 5; 
        theG [0] = 7; 
}

int main() 
{ 
    int size;
    int MM [2] = {1000, 0}; 
    int *theG = NULL; 
    cout << "\nPlease insert size of array:" << endl; 
    cin >> size; 
    theG = new int [size];  
    findMM(MM, theG);
    cout << MM [1] << endl << theG[0];    
    delete [] theG;     
    return 0; 
}

输出正确的是 5 和 7?

【问题讨论】:

  • 一个数组已经是一个指针。在调用 findMM 时从 &MM 中删除 &。
  • @TheSavage 这不是真的!
  • 你能不能给我一个更详细的答案,原因只是让我感到困惑......
  • @TheSavage 数组不是指针。数组衰减指向数组第一个元素的指针。
  • 你打算在 C 程序中也使用findMM 吗?如果不是,为什么要使用原始数组而不是 stl 容器?如果不将theG 内的theG 的大小(大小)作为参数传递,你怎么知道它的大小?

标签: c++ arrays pointers reference


【解决方案1】:

由于MM 是一个数组,&amp;MM 是一个指向数组的指针(即您在错误中看到的int (*)[2] 类型)。相反,您似乎想将指针传递给数组的第一个元素。有两种方法可以做到这一点。首先,您可以显式获取第一个元素,然后获取它的地址:&amp;MM[0]。其次,您可以依靠数组到指针的转换来为您完成,只需传递MM。数组到指针的转换将数组转换为指向其第一个元素的指针。

【讨论】:

  • 但是如果我想传递整个数组,因为最终在函数结束时我想将 MM [0] 和 MM [1] 更改为不同的值,那么我必须传递两个地址吗?
  • @user3207442 不,您可以像普通数组一样使用operator[] 访问数组的元素。
  • @user3207442 不知道。你知道a[x] 等于*(a + x) 吗?因此,如果a 是指向数组中第一个元素的指针,a[1] 将执行*(a + 1),它会为您提供数组中的第二个元素。魔法!
  • 好的,让我重新表述一下,以确保我理解。如果我说在函数中放入 PMM [0] = 5,然后在 main 中放入一个 cout 语句,即 cout
  • @user3207442 正确。如果你在函数中做PMM[1] = 5;,然后cout &lt;&lt; MM[1];,你会再次得到5。
【解决方案2】:

我知道这个问题已经得到解答,但我相信我可以为提问者的理解做出贡献。

让我们从基础开始:

void main()
{
    int a = 2; // a is an int
    cout << a << endl; // print 2

    int *b; // b is a pointer-to-int
    b = &a; // store the address of a in b
    cout << *b << endl;// print the value that b points to, which is 2

    int my_array = new int[3]; // allocate an array with 3 integers
    my_array[0] = 50; // store 50 in the first element of the array
    my_array[1] = 51; // store 51 in the second element of the array
    my_array[2] = 52; // store 52 in the third element of the array
    cout << c[0] << endl; // print 50

    some_function(my_array, 3); // explained below
}

现在让我们看看如何将数组传递给函数。假设我们想要一个名为some_function 的函数来接收一个数组。

void some_function(int *some_array, int size_of_the_array)
{
    // use the array however you like here
}

函数some_function 接收一个指向int 的指针(也称为“pointer-to-int”)。数组的名字总是它的第一个元素的地址,所以如果一个函数需要一个指向int的指针,而你给它一个ints的数组的名字,你实际上是给它的地址数组中的第一个元素(这只是 C++ 语法规则)。所以这个函数现在有了数组中第一个元素的地址,它可以做*some_array之类的东西来访问数组中的第一个元素,但是如果它想访问其他元素怎么办?它将 1 添加到它已有的指针上,然后将 * 运算符应用于它:*(some_array + 1)。假设int 是 4 个字节,如果将 1 加到一个指向 int 的指针上,这个加法的结果是一个新指针,它指向内存中 4 个字节前面的位置,所以*(some_array + 93) 是数组some_array 的第 94 个元素(数组元素按顺序存储在内存中)。对此的简写符号是some_array[93]。所以如果你有int *some_array = new int[100];,那么some_array是一个指针,some_array[93]*(some_array + 93)一样,是数组中的第94个元素。

虽然地址本身还不够,但您还需要知道数组中的条目数,这样您就不会尝试访问超出数组末尾的元素。在此示例中,假设 some_function 仅打印数组的内容,因此如果您不提供 3 作为函数的第二个参数,那么它将无法知道何时停止向它收到的指针添加 1在第一个论点中。但是请注意,通过这种方式将数组传递给函数,您并没有将数组的副本传递给函数,您只是告诉它在内存中的哪里可以找到它的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多