how arrays are treated by gcc [ question related to C programing ] -


this compiles in gcc no errors or warnings -wall option meaning array bounds checked @ run-time , hence compiler can't detect error

#include<stdio.h>  int main() {       int a[2][3][4];        a[1][2][100] = 4 ;        return 0; } 

however,

#include<stdio.h>  int main() {       int a[2][3];        a[1][2][100] = 4 ;        return 0; } 

this generates error while compiling :

$ gcc sample.c -wall sample.c: in function ‘main’: sample.c:7: error: subscripted value neither array nor pointer 

why ? in both 2 codes a[1][2][100] invalid . still how can compiler detect code2 , not in code1.

specially when every compiler flattens multidimensional array corresponding single dimension arrays, how can compiler selectively aware of flaw in code.

explanation or mention of book or article proper explanation resides gratefully accepted :)

the difference types. c no bounds checking (static) type checking. type of in first example int[][][] type in second example int[][].

the "flattening" refer happens in code generation, (conceptually, @ least) after type checking.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -