thanks,excellen t work.
Arrays Revealed
Collapse
X
-
-
what a nice article!!!! i'm just curious if to locate the value of the 1st member of the array, you would type distance[0], how about if i used in the 1st member will be distance[1] instead of zero, the program will be still run in c++?Comment
-
Is your array named distance? If so, distance[0] is the value of the first element. distance[1] is the value of the second element.
Try it out. It will run in both C and C++.Comment
-
-
It was excellent article i had lot of confusion in multidimensiona l arrays, it all got cleared. But it would have been great if explained that after passing the multidimensiona l array as argument to a function how do you access the elements of the array inside the function.Comment
-
-
Just remember that arr is the address of element 0. It doesn't matter if element 0 is another array. Now when you use pointer arithmetic, like arr + 1, you take the address of the array and increment it by the sizeof element 0, So arr + 1 is the address of element 1.
When you use &arr you are asking for the address of the array. arr is the address of element 0. By definition the address of an array is the address of element 0 so &arr and arr will be the same.Comment
-
-
I have one confusion in below code :
IntArray5Ptr func(int arg)
{
int (* temp)[5] = new int[arg][5];
return temp;
}
int main()
{
int (* arrA)[5] = func(4);
}
You can return a pointer to type in addition to returning a type.
So you could define a type to be an element of your array (in the example this is an array of 5 int)
and return a pointer to that type:
typedef int IntArray5[5];
IntArray5* funcB(int arg)
{
int (* temp)[5] = new int[arg][5];
return temp;
}
int main()
{
int (* arr)[5] = func(4);
}
You said that "you could define a type to be an element of your array (in the example this is an array of 5 int)"
Now here temp holds the address of the element 0 of array[4][5],which itself is an array of 5 elements , so when we return temp in the second case , how can we return a pointer to a type ?
for this scenario , we even must have the element of the array to be a pointer to an array of 5 int but the element of the array is an integer value .
Please explain this briefly .Comment
-
First, a pointer is a variable that contains an address. So all we are doing is returning an address. Like int* or char*. That means int (* temp)[5] is just a pointer and it would contain the address of element 0 of the 5 element array of int it points to.
Second, what hurts here is that the function allocated an array of 5 int arrays based on arg but all the function returns is an address and not the number of elements. That means this technique will not work since the function must return the address of the arrays plus the number of elements.
One way out of this is to have funcB() allocate just the correct number of bytes and return the address of these bytes as a void*. Then the calling function can typecast the void* to a pointer to an array of 5 int. Since the calling function provided the number of bytes, it could also divide that number by the sizeof(int) * 5 to obtain the number of elements.
Microsoft COM got at this by using a struct called a VARIANT which contains the starting address, number of elements, and a thing called a discriminator which is used to determine the type of the elements in the array.
C++ uses library code called a vector which lets you generate code based on the contents of the array.
All this is by way of saying that array handling can be difficult. Knowing that, try to keep your designs simple to avoid complex issues like above.Comment
-
But using typedef , you already defined that you are returning the address of the array of 5 integer , so why to put extra * on the type name IntArray5 in second case , what do we achieve through this extra * here ?Comment
-
You probably mean this:
Code:typedef int IntArray5[5]; IntArray5* funcB(int arg) { int (* temp)[5] = new int[arg][5]; return temp;
If funcB reurns IntArray5 you will get a compile error that your function is trying to return an array. You can only return a type or a pointer to a type. An array is neither of these so you can't return it. Never mind that IntArray5 is the address of element 0. Your compiler just does not care. So you add the asterisk to make the compiler happy.
The other thing you can do is change your typedef to::
Code:typedef int (*IntArray5)[5];
Comment
-
Now I got it , just last doubt , can you please explain this statement with an example :
There are no rvalues of array type in C. There are pointer lvalues and rvalues, integer lvalues and rvalues, structure lvalues and rvalues etc... But only lvalue arrays. When you try to convert an lvalue of array type to an rvalue, you no longer have an array, you have a pointer to the array's first member.
I am unable to get what is the meaning of rvalue of array type and lvalue of array type ?As far as I know , lvalue or rvalue corresponds to only expression so then how are we linking it to array type ?Comment
-
This is from your code:
Code:int(*temp)[5]= new int[arg][5];
You cannot tell from temp how many arrays of 5 int were allocated.
To know how many elements are in the array it must be an lvalue:
Code:int temp[100][5];
Comment
Comment