“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
int (*a)[10]; represents the declaration of a pointer to an array of ten integers. So the value of a is initially some address allocated by the compiler and don’t rely on the fact the address is 0 as it happens for static variables. The value can be zero if you add the “static” keyword in front of declaration but I don’t advise you to further use this pointer to access some elements.
If the integer is n bytes ( 2 or 4 depending on the language) it is true that the value of a will be increase with 10*n.
Test this program to understand:
#include <stdio.h>
void main(int argc,char*argv[]) { int b[10]={1,2,3,4}; int (*a)[10]; printf(”%pn”,a); printf(”%dn”,(*a)[0]); a++; printf(”%pn”,a); a=&b; printf(”%pn”,a); printf(”%dn”,(*a)[0]); }
If the expression has been:
int *a[10];
a++;
Then this would be a 10 sized pointer-array to integers which are not defined yet.’a’ points to the first adress which holds a pointer to integer.When a++ executes the sizeof POINTER type will be added to a and this will point to the second element of the pointer-array.
But the expression is:
int (*a)[10];
This expression is garbage.Compiler would not generate code for it.It is a function pointer which return int[10], so it is nonsense.Also following code is same:
int (* a(float h))[10];
It will point to illeagal value. Because we cant grow an array dynamically. I it is leagal to use becoz compiler does not ruturn error. But logically it it incorrect.
The newer version of C++ does not allow this declearation as it does not make any sense and is useless.
20 assuming that the size of an int is 2
int (*a)[10]; represents the declaration of a pointer to an array of ten integers. So the value of a is initially some address allocated by the compiler and don’t rely on the fact the address is 0 as it happens for static variables.
The value can be zero if you add the “static” keyword in front of declaration but I don’t advise you to further use this pointer to access some elements.
If the integer is n bytes ( 2 or 4 depending on the language) it is true that the value of a will be increase with 10*n.
Test this program to understand:
#include <stdio.h>
void main(int argc,char*argv[])
{
int b[10]={1,2,3,4};
int (*a)[10];
printf(”%pn”,a);
printf(”%dn”,(*a)[0]);
a++;
printf(”%pn”,a);
a=&b;
printf(”%pn”,a);
printf(”%dn”,(*a)[0]);
}
To octav_timofte:
Just run your program under VC++ 2005 Express Edition, it showed me an error, “The variable ‘a’ is being used without being defined”.
I am not sure what compiler you use, but I think int (*a)[10] only declares a pointer, not initialized. a++ is illegal.
Correct me if not right.
a++ is not illegal. It will just point to data out of
array range. Using it to access data out of array range will be illegal.
If the expression has been:
int *a[10];
a++;
Then this would be a 10 sized pointer-array to integers which are not defined yet.’a’ points to the first adress which holds a pointer to integer.When a++ executes the sizeof POINTER type will be added to a and this will point to the second element of the pointer-array.
But the expression is:
int (*a)[10];
This expression is garbage.Compiler would not generate code for it.It is a function pointer which return int[10], so it is nonsense.Also following code is same:
int (* a(float h))[10];
I tested the following code in Microsoft Visual C++.
int a[4] = {1, 2, 3, 4};
a++;
I got error that “++ need l-value”.
Why is that?
It should be like *(a++)
then, a[1] is the answer.
a++ will give the (address of a + 40)
It will point to illeagal value. Because we cant grow an array dynamically. I it is leagal to use becoz compiler does not ruturn error. But logically it it incorrect.
The newer version of C++ does not allow this declearation as it does not make any sense and is useless.
Leave an Answer/Comment