+ 2

Can anyone explain the behavior of this code

#include <stdio.h> int main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p, *q; p = &a[2][2][2]; *q = ***a; printf("%d %d", *p, *q); return 0; } Why garbage value and segmentation fault is coming

24th Nov 2019, 7:31 AM
Preity
Preity - avatar
3 ответов
24th Nov 2019, 8:04 AM
Preity
Preity - avatar
0
Is the code right ? am confused, a[2][2][2] here you just have 1 row 2 column and 4 values.. This should result in warning.. correct me if am wrong
24th Nov 2019, 7:58 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
You are reading off the end of the array. Remember that arrays are indexed beginning from 0. You made an array of size 2 and you are reading the 3rd element.
24th Nov 2019, 10:33 AM
Schindlabua
Schindlabua - avatar