If we declare: int a[4][3]; then a[i][j] is equivalent to:
1) *(a[i] + j)
2) *(&a[0][0] + 4*i + j)
3) Both (a) and (b)
4) None of the above
Can anyone enlighten me on which is the correct ans and
why !! thanks
If we declare: int a[4][3]; then a[i][j] is equivalent to:
1) *(a[i] + j)
2) *(&a[0][0] + 4*i + j)
3) Both (a) and (b)
4) None of the above
Can anyone enlighten me on which is the correct ans and
why !! thanks
I'll give you two hints...
1. an array name is actually a pointer to the first position in the array
2. A multidimensional array is in reality an array of arrays. And to dig in even further, a multidimensional array is in fact a single dimension array. arr[2][3] is in fact an array with 6 elements
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Really? Does arr[1][1] equal ((int*)arr)[4]?Originally Posted by Mario F.
A multidimensional array isn't usually a single dimension array. Consider argv. Each argument is a different length.
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
Yes. I should have rephrased that better. I meant conceptually since,
produce the same array.Code:int arr[4][3] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} }; //and int arr[4][3] = {1,2,3,4,5,6,7,8,9,10,11,12};
The second definition helps making it clear which answer is the right one.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
> Consider argv. Each argument is a different length.
True, each argument is a different length, but you are a little confused about argv. argv is an array of pointers to strings.
You can find the type of a declaration by reading parentheses (if there are any) inside out and right to left.
arr[2][3], and arr[6] produce essentially the same array. In memory there is actually no difference. C++ strictly speaking doesn't know multi-dimension arrays.
Besides b) is wrong
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
hi mario. from ur explaination 1) is rt
but for *(&a[0][0] + 4*i + j)
i am not really sure.
my feeling is that it is wrong .
and the rt ans would be a[i][j] =*(&a[0][0] + 3*i + j)
is this correct??
almost
*(&a[0][0] + 4*i + j -1)
Which makes it a too complicated formula.
You are correct. a) is right. All others are wrong
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
> but for *(&a[0][0] + 4*i + j)
Right away, this is dereferencing a memory address, which will make the program explode. It is wrong.
I think it is more like the computer does not know about multi-dimensional arrays, and C++ initializer syntax allows one to take advantage of that as a shorthand. C++ does know about multi-dimensional arrays.arr[2][3], and arr[6] produce essentially the same array. In memory there is actually no difference. C++ strictly speaking doesn't know multi-dimension arrays.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
hi guys i have written a prog that proves 2)
is correct. the output is 1.
IS there anything wrong with my prog??
Code:#include <iostream.h> #include <stdlib.h> #include <stdio.h> int a[4][3],i,j; int test(); main(void) { printf("%i",test()); cin.get(); return 0; } int test() { if (a[i][j]==*(&a[0][0] + 4*i + j)) return 1; return 0; }
Yes. Try initializing your variables first.
Also, it looks like you're using an out-of-date compiler (if that actually compiles)
Code:#include <iostream> int a[4][3] = { {1,2,3} , {4,5,6} , {7,8,9} , {10,11,12} }; int i=1,j=1; int test(); int main(void) { std::cout<<test(); std::cin.get(); return 0; } int test() { if (a[i][j]==*(&a[0][0] + 4*i + j)) return 1; return 0; }
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
Originally Posted by JaWiB
actually i jus started picking up c++ a week ago and i am using DEV C++ to do the compiling. not sure which is gd for beginner.
i started programing with JAVA which i feel is easier to understand
hmm... suppose i = j = 0.almost
*(&a[0][0] + 4*i + j -1)
Which makes it a too complicated formula.
*(&a[0][0] + 4*0 + 0 - 1) => *(&a[0][0] - 1)
This looks like one is trying to dereference a pointer that points to one before the first element.
I think it is okay. This is from section 5.3.1 of the C++ Standard:Right away, this is dereferencing a memory address, which will make the program explode. It is wrong.
"The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is "pointer to T," the type of the result is "T.""
and a few lines later:
"The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualifiedid. In the first case, if the type of the expression is "T," the type of the result is "pointer to T.""
In this case a[0][0] is an lvalue, so &a[0][0] is valid. (&a[0][0] + 4*i + j) is just pointer arithmetic, and results in a pointer. Hence using unary operator* on it should be valid, assuming that the pointer is valid.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If you picked up C++ a week ago, why are you writing everything in C, anyway?