I learned that:
char ar[]
is the same of
char *ar
These three expressions:
char ar[][] //1
char *ar[] //2
char **ar //3
are the same thing for the compiler?
These two expressions:
char ar[]
char ar[][]
will allocate the array on the stack, while all the others will allocate it on the heap?
char ar[]is only the same aschar *arwhenaris a function parameter. Otherwise they are an array and a pointer respectively.char ar[][]is a 2-d array ifaris not a function parameter. Otherwise it’s a pointer to a 1-d array.char *ar[]is a 1-d array of pointers ifaris not a function parameter. Otherwise it’s a pointer to a pointer.char **aris a pointer to a pointer.Basically, if it’s a function parameter and it looks like it’s an array, it’s actually a pointer to the array’s first element. Arrays aren’t passed in their entirety as function parameters. When you try to, you will pass pointers to first elements of the arrays, and not arrays themselves.
All variables defined outside of functions aren’t neither in the heap nor on the stack. They are global variables.
All variables defined inside of functions (with the exception of
staticvariables) are on the stack.staticvariables are global-ish, they aren’t neither in the heap nor on the stack.staticreduces the visibility of a global variable to the function or module scope, only that.Only those variables allocated explicitly via
malloc(),calloc(),ralloc()live in the heap. Some standard library functions may create variables/objects in the heap, e.g.fopen().