how to call a function with a pointer as its argument

0

what is problem? i want send a multi-dimensional pointer to a function as its arguments. But i got this error

Exception thrown at 0x00007FF770A92231 in Check nevis.exe: 0xC0000005: Access violation reading location 0x0000000400000007.

what did i do wrong? and what this error means?

#include <stdio.h>
test(int *ptr)
{
    printf("%d", *(*(ptr+1)+1));
}
int main()
{
    int a[2][3] = { { 1,2,3 }, { 4,5,6 } };
    int(*ptr)[2][3]=&a;
    test(ptr);
    return 0;
}
c
pointers
exception
multidimensional-array
asked on Stack Overflow Dec 7, 2019 by Asadiyan • edited Dec 8, 2019 by VillageTech

1 Answer

-1

You need to have pointer to pointer to int:

void test(int **ptr)
{
    printf("%d", ptr[1][1]);
}

int main()
{
    int a[2][3] = { { 1,2,3 }, { 4,5,6 } };
    int **ptr = a;
    test(ptr);
    return 0;
}

Note: it is expected and good practice to prefix the function, which returns nothing, with 'void'. Probably you got a warning about it - NEVER ignore warnings!

answered on Stack Overflow Dec 7, 2019 by VillageTech

User contributions licensed under CC BY-SA 3.0