C - Segmentation fault with pointer to array -
i'm hoping can me little piece of code. it's stupid test, dont know doing wrong. it's this:
#include <stdio.h> int **ipp; int ventas [3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; int main(void){ ipp = (int **)ventas; printf("%d\n", **ipp); return 0; }
it compiles (i'm using gcc), when execute keep getting segmentation fault. doing wrong? think has un-initalized pointer, 'ventas' array initialized, , it's assigned **ipp.
a pointer-to-pointer not array. nor pointer 2d array. aren't slightest compatible. forget pointer-to-pointers in case.
if want pointer 2d array, must write
int (*ipp)[3][4] = &ventas;
you can't print pointer using
%d
format specifier. should use%p
.
corrected code printing address of 2d array:
#include <stdio.h> int main(void){ int ventas [3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; int (*ipp)[3][4]; ipp = &ventas; printf("%p\n", ipp); return 0; }
Comments
Post a Comment