指向c ++函数中的数组

时间:2022-09-01 11:27:09

I am a beginner to c++. Pointer is quite confusing to me. Especially on how to use it in functions and array. I tried to create a pointer to array in function and just output it. However it keeps giving me the address of the array instead of the value.

我是c ++的初学者。指针对我来说很混乱。特别是关于如何在函数和数组中使用它。我试图在函数中创建一个指向数组的指针,然后输出它。但它不断给我数组的地址而不是值。

void testing(int* arr){
    cout << arr << endl;
}

int main()
{
    int my_arr[]{ 4,7,1 };
    testing(my_arr);

    string y;
    getline(cin, y);
    return 0;
}

I tried using testing(&my_arr); to output value but it give me errors:

我尝试过使用测试(&my_arr);输出值,但它给我错误:

  • argument of type "int (*)[3]" is incompatible with parameter of type "int *
  • “int(*)[3]”类型的参数与“int *”类型的参数不兼容

  • 'void testing(int *)': cannot convert argument 1 from 'int (*)[3]' to 'int *'
  • 'void testing(int *)':无法将参数1从'int(*)[3]'转换为'int *'

Thanks a lot for any help!

非常感谢您的帮助!

3 个解决方案

#1


0  

for printing the arrays, you can either use the array index or pointers arithmetic. The test function could also be written as

要打印数组,您可以使用数组索引或指针算法。测试功能也可以写成

void testing(int* arr, int len) {
    for (int ctr = 0; ctr < len; ctr++) {
        std::cout << *(arr + ctr) << std::endl;
    }
} 

int main()
{
    int my_arr[]{ 4,7,1 };
    testing(my_arr, 3);

    return 0;
}

#2


1  

To print the values in an array rather than the starting address, you need to use a loop.

要在数组中打印值而不是起始地址,您需要使用循环。

#include <iostream>
#include <string>

// note extra param for length of array.
void testing(int* arr, int len){
    for (int i = 0; i < len; ++i)
        std::cout << arr[i] << " ";
    std::cout << "\n";
}

int main()
{
    int my_arr[]{ 4,7,1 };
    testing(my_arr, 3);

    return 0;
}

You can't pass testing(&my_arr) because &my_arr is of type int (*)[] as per the error message you received. That is not the same as int*.

您无法通过测试(&my_arr),因为&my_arr的类型为int(*)[],根据您收到的错误消息。这与int *不同。

#3


0  

In testing() you are trying to use arr element without its index. Here arr is the only base memory address of that memory. To get value from there you have to specify index.

在testing()中,您尝试使用没有索引的arr元素。这里arr是该内存的唯一基本内存地址。要从中获取价值,您必须指定索引。

void testing(int* arr, int len)
{
    for(int i = 0; i < len; i++)
    {
        cout << arr[i] << endl;
    }
}

In main() you can pass a length of an array.

在main()中,您可以传递一个数组的长度。

int main()
{
    int my_arr[]{ 4,7,1 };
    testing(my_arr, sizeof(my_arr) / sizeof(int));
    return 0;  
}

#1


0  

for printing the arrays, you can either use the array index or pointers arithmetic. The test function could also be written as

要打印数组,您可以使用数组索引或指针算法。测试功能也可以写成

void testing(int* arr, int len) {
    for (int ctr = 0; ctr < len; ctr++) {
        std::cout << *(arr + ctr) << std::endl;
    }
} 

int main()
{
    int my_arr[]{ 4,7,1 };
    testing(my_arr, 3);

    return 0;
}

#2


1  

To print the values in an array rather than the starting address, you need to use a loop.

要在数组中打印值而不是起始地址,您需要使用循环。

#include <iostream>
#include <string>

// note extra param for length of array.
void testing(int* arr, int len){
    for (int i = 0; i < len; ++i)
        std::cout << arr[i] << " ";
    std::cout << "\n";
}

int main()
{
    int my_arr[]{ 4,7,1 };
    testing(my_arr, 3);

    return 0;
}

You can't pass testing(&my_arr) because &my_arr is of type int (*)[] as per the error message you received. That is not the same as int*.

您无法通过测试(&my_arr),因为&my_arr的类型为int(*)[],根据您收到的错误消息。这与int *不同。

#3


0  

In testing() you are trying to use arr element without its index. Here arr is the only base memory address of that memory. To get value from there you have to specify index.

在testing()中,您尝试使用没有索引的arr元素。这里arr是该内存的唯一基本内存地址。要从中获取价值,您必须指定索引。

void testing(int* arr, int len)
{
    for(int i = 0; i < len; i++)
    {
        cout << arr[i] << endl;
    }
}

In main() you can pass a length of an array.

在main()中,您可以传递一个数组的长度。

int main()
{
    int my_arr[]{ 4,7,1 };
    testing(my_arr, sizeof(my_arr) / sizeof(int));
    return 0;  
}