2

We currently learn about pointers in combination with arrays in C/C++ and have to implement some function, for which we have a given function call. The objective is to print out the array values by calling the function with the array adress. The output is not what was expected.

I tried printing the the array values with & and * as well as with none of those.

void ausgabe1D_A(double (*ptr1D)[4]){
    int i{0};
    for (i = 0; i < 4; i++)
        std::cout << *ptr1D[i] << "  ";
}

int main()
{
    double ary1D[4] = {1.1, 2.2, 3.3, 4.4};

    ausgabe1D_A(&ary1D);
}

I expected the output to be:
1.1 2.2 3.3 4.4

Instead I got:
0x61fdf0 0x61fe10 0x61fe30 0x61fe50 (with and without &)
1.1 9.09539e-318 0 0 (with *)

EDIT: Sorry if that wasn't clear, but we have to call the function with &ary1D. We are trying out different ways and your ways are coming up in the exercise in following functions but for now we need it with the &-Operator.

4 Answers 4

2

Although others provide a possible solution, I will try to explain why your code fails. Expression double(*ptr1D)[4] refers to a pointer to an array of four elements (doubles). To get values, you should firstly dereference the pointer and only then print out its n-th value, as follows:

void ausgabe1D_A(double (*ptr1D)[4])
{
  for (int i = 0; i < 4; i++) {
    std::cout << (*ptr1D)[i] << " "; // <-- note the parenthesis
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, this answer is what I've been looking for! Did i get it right, that I had a pointer pointing on an array which essentialy is a pointer and that's why the first value of the array was printed correctly while the others were not?
@xCuse The problem was probably that *ptr1D[i] points to base of array + i * length of whole array. Hence, the out-of-bound accesses. Demo on coliru
An array decays to a pointer to the first element. So ary1D decays to a pointer to a double. Going the other ways double (*ptr1D)[4] is what a two dimensional array decays to. Your input (with the & operator) is a 1x4 two dimensional array. *(ptr1D[i]) [parents for emphasis] then prints the first double of each row. Since your input only has one row the rest are garbage, out of bounds array access.
2

When you reference an array by its name, you are referencing it by its pointer, so you function argument is wrong. It should be like

void ausgabe1D_A(double *ptr1D, size_t size){
    int i{0};
    for (i = 0; i < size; i++)
        std::cout << ptr1D[i] << "  ";
}

int main()
{
    double ary1D[4] = {1.1, 2.2, 3.3, 4.4};

    ausgabe1D_A(ary1D,4);
}

Here is a picture of the memory of a short integer array for example

enter image description here

4 Comments

Hey, I'm sorry that i didn't make it clear but I need to call the function with &ptr1D
@XCuse, no! ary1D is a pointer. it's like &(ary1D[0]). Arrays are pointers to an array of the type.
Thank you, we already learned that an array is nothing else than a pointer to an adress where the array element 0 is saved in and that ary[i] is equal to *(ary+i). Nonetheless I had to use the given parameter &ary1D. And sorry it shoudl have been &ary1D not ptr1D.
If using &ary1D is mandatory, then you can reference it like (*ary1D)[i].
0

You can take advantage of the fact that arrays can decay into pointers, so you'd write something like

#include <iostream>

void ausgabe1D_A(double* ptr1D){
    for (std::size_t i = 0; i < 4; i++)
        std::cout << ptr1D[i] << "  ";
}

int main()
{
    double ary1D[4] = {1.1, 2.2, 3.3, 4.4};
    ausgabe1D_A(ary1D);
}

The way this works is that the pointer ptr1d is essentially the address of the first element of the array.

1 Comment

Hey thank you for your answer! Unfortunately we have to call the function like this ausgabe1D_A(&ary1D);
0

I think you are looking for way to pass using & operator. Here you go.

void print( int (*arr)[4])
{
    int *d = *arr;
    for(int i = 0 ;i <4 ; ++i)
    {
        std::cout<<d[i]<<std::endl;
    }
}
int main()
{
    int arr[] = {1,2,3,4};
    print(&arr);

}

Let me know if you found it hard to understand.

1 Comment

Thank you, this helps a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.