shaanxxx said:
what is the problem with following programme.
int * myMalloc(int Size)
{
int a[Size];
return a;
}
can we use sizeof operator to find sizeof array 'a';
Thanks
shaan.
In C++ and pre-C99 versions of C, an array's size must be a constant
integral expression so that it can be calculated at compile-time. In
C99, this restriction was relaxed. An array's size must be an integral
expression, not necessarily a constant one. This allows you to declare
a variable-length array or an array whose size is determined at
runtime. For example:
void func(int sz)
{
int arr[sz]; // sz ins't const; allowed only in C99
}
The size of arr may be different for every func() call. For example:
int main()
{
int size;
printf("enter array's size : ");
scanf("%d", &size);
func(size);
}
To support variable-length arrays, C99 adds the notation [*] in a
function's parameter list to indicate a variable-length array. In
addition, C99 extends the functionality of sizeof. When applied to a
variable-length array, it calculates the its size at runtime. In my
opinion, this is the most daring change in C99. Not only does it alter
the semantics of a built-in operator from compile-time to runtime
operation (only when using variable-length arrays), it's also a "quiet
change" -- there's no explicit syntactic clue to distinguish between
the two forms.
ref:
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=215&rl=1