-3

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.

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:

  • argument of type "int (*)[3]" is incompatible with parameter of type "int *
  • 'void testing(int *)': cannot convert argument 1 from 'int (*)[3]' to 'int *'

Thanks a lot for any help!

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
skyzhuzhu
  • 45
  • 1
  • 8
  • 1
    The error tells you everything you need to know. An `int*` is not a pointer to an int array. – Charles Sep 15 '17 at 02:59
  • 3
    Are you sure you aren't calling the function with `testing(&my_arr);`? That's what the error suggests to me. – Ken Y-N Sep 15 '17 at 03:03
  • @KenY-N you're saying that an `int*` is the same as a *pointer* to an int array? You're mistaken. A pointer to an int array is `int**` or `int *[]` as shown in the error. – Charles Sep 15 '17 at 03:12
  • 1
    "it keeps giving me the address of the array instead of the value". That's because the address (of the first item, which is the same as the address of the array) is what you pass to the stream. It outputs exactly what you tell it to output. To output the array values use a loop. It will need to know the number of items. – Cheers and hth. - Alf Sep 15 '17 at 03:12
  • @KenY-N, yes for the errors i use `testing(&my_arr);` instead because `testing(my_arr);` gives me address. – skyzhuzhu Sep 15 '17 at 03:12
  • OP, if you want to print the values in an array you need to loop to print them out. `std::cout` won't do that for you – Charles Sep 15 '17 at 03:13
  • @Charles Oops, I'm hopelessly confused... Sorry. – Ken Y-N Sep 15 '17 at 03:14
  • @Charles, how to create pointer to array? I see tutorials they teach int* arr – skyzhuzhu Sep 15 '17 at 03:15
  • @engkhsky `int*` is ___not___ a pointer to an array! It's just a pointer to an `int`. – Charles Sep 15 '17 at 03:20
  • 1
    Read about [decaying](https://stackoverflow.com/questions/1461432/what-is-array-decaying) – Passer By Sep 15 '17 at 03:23
  • There's no built-in function for outputting all of the members of a container (except string...) , you will need to make your own – M.M Sep 15 '17 at 05:23

3 Answers3

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*.

Charles
  • 1,384
  • 11
  • 18
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;
}
Daksh Gupta
  • 7,554
  • 2
  • 25
  • 36
  • [No need for std::endl](https://stackoverflow.com/questions/35580919/should-stdendl-always-be-used) – Charles Sep 15 '17 at 16:16
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.

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.

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