Who is to say that memory layout, pointers & all that isn't confusing?
Hey, everything is confusing in a way.
Not I. But that's not to say that it's something that shouldn't be learned early. I skipped most of that (pointers, memory stuff), in class when I first started programming and to this day I sometimes have to think about how to use and implement them properly in code for a little bit. I shouldn't have to, but there you go. So from my experience I figure the earlier you know it the better.
Edit: As you said. There's a difference in the school of thought. I don't think you're going to convince me one way's better than the other here. I never did consider learning about containers before arrays and other such nuances, but that's because it was how I was taught.
Last edited by twomers; 09-30-2008 at 02:34 PM.
From my experience, before I learned all the advanced stuff and techniques available with C++, I did the old "C" way, with overly complex code that even sometimes relied on certain implementation.
Had I learned of these "higher" level things first, I probably would never have done that in the first place and the old code would have been better.
My last remark for this discussion is that the overly complex design might provide you with a deeper understanding of all those things we were talking about work, which can only be good. As I said in my edit to my last post (if you read it after I posted it), I always believed (and still do), that learning from a reasonable 'bottom' up is the way to go.
And again I am furious with C++. There is no way of predicting how to point to something. I would never in my lifetime guess
that (*)... is that a typecast?Code:int (*)[3][2] p = x;
I know the index of first dimension is optional but the parentheses with deferencing operator? And why do dimensions go before the pointer name
when in an array declaration they go after?
I have issues with pointers! function pointers, array pointers, all pointers.
Was Bjarne Strostrup smoking crack when he was developing the syntax?
I swear to almighty one i'll start learning calculus instead of C++
We're all Fu##ed aren't we?
>> And why do dimensions go before the pointer name when in an array declaration they go after?
Because it's a pointer to the array. The array's memory is already there. You have to specify that the pointer points to that memory. You know that int *ptr; makes makes a pointer called ptr. The syntax extends beyond that in case you want to point to an array. There's no sense putting them after the variable name. That'll cause an array of pointers. Make sense? It's just something you have to wrap your head around. This is useful guide about stuff (relevant part copied below).In short, it's like how int **ptrptr; is a pointer to a pointer, but the second star is replaced by an array specifier.5. Using the variable a, give definitions for the following:
a) An integer
b) A pointer to an integer
c) A pointer to a pointer to an integer
d) An array of 10 integers
e) An array of 10 pointers to integers
f) A pointer to an array of 10 integers
g) A pointer to a function that takes an integer as an argument and returns an integer
h) An array of ten pointers to functions that take an integer argument and return an integer
The answers are:
a) int a; // An integer
b) int *a; // A pointer to an integer
c) int **a; // A pointer to a pointer to an integer
d) int a[10]; // An array of 10 integers
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers
g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer
h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer
>> I have issues with pointers! function pointers, array pointers, all pointers.
All about pointers. I can't remember if it does function pointers or not...
>> I swear to almighty one i'll start learning calculus instead of C++
Do both and you can be cool too!
Edit: In fact, I believe Elysia's syntax is incorrect... it should be int (*p)[3][2], I believe, and not int (*)[3][2] p = x;e.g.:Code:int main( void ) { int myarray[4][3][2] = { NULL }; int (*p)[3][2] = &myarray[0]; std::cout<< myarray[0][0][0] << "\n"; *p[0][0] = 42; std::cout<< myarray[0][0][0]; return 0; }
Last edited by twomers; 09-30-2008 at 03:02 PM. Reason: Typo
It's the pointer marker! It says it's a pointer.
You don't necessarily need to use them that often in C++, though :)I have issues with pointers! function pointers, array pointers, all pointers.
You're blaming the wrong individual. It's a relic from C, so blame C!Was Bjarne Strostrup smoking crack when he was developing the syntax?
Of yes, you're right.
Dang. Didn't check that. It's not very often I use pointers to arrays.
Oh yes, I just saw it now.
Well, to be honest, I don't know which way is "right," so I don't really have any convincing to do! :)
OK..
I can live with
It is somewhat consistent with other pointer declarations.Code:int (*p)[3][2] = x;
But than again... how do i figure out when do I need parentheses when declaring pointers in general?
When declaring a pointer to one dimensional array I don't need (* )
I also noticed similar thing with function pointers where you put (* ) around the
name of the pointer!
We're all Fu##ed aren't we?
You need parentheses, you just apparently haven't been using them.
You need parentheses whenever you have to over-ride binding rules. Normally the * will bind with the type (int in this case), rather than the name. So similarlyCode:int *bob[6]; //an array of 6 pointers-to-int int (*george)[6]; //a pointer-to-array-of-int
Code:int *bob(int x); //a function taking int returning pointer-to-int int (*bob2)(int x); // a pointer to a function taking int and returning int int *(*bob3)(int x); // a pointer to a function taking int and returning pointer-to-int
well no... I do not need parentheses for one dimentional arraysYou need parentheses, you just apparently haven't been using them.
But..Code:int main(void){ int v[3]={3,2,3}; int *b=v; cout<< b[1]; cin.get(); }
Gives an errorCode:int main(void){ int v[3][2]={3,2,3,3,2,3}; int *b [2] =v; cout<< b[1][2]; cin.get(); }
We're all Fu##ed aren't we?
But your code seems to suggest otherwise!
Another good reason to use T* var instead of T *var.
So similarly
Code:int *bob(int x); //a function taking int returning pointer-to-int int (*bob2)(int x); // a pointer to a function taking int and returning int int *(*bob3)(int x); // a pointer to a function taking int and returning pointer-to-intSee? See!?! Much easier to read!Code:int* bob(int x); //a function taking int returning pointer-to-int int (* bob2)(int x); // a pointer to a function taking int and returning int int* (* bob3)(int x); // a pointer to a function taking int and returning pointer-to-int
This isn't a pointer to an array.
But this is!Code:int main() { int v[3]={3,2,3}; int (* b)[3] = v; cout<< b[1]; cin.get(); }
And please - drop the void from main. It's not necessary in C++.
gives me an errorCode:int main() { int v[3]={3,2,3}; int (* b)[3] = v; cout<< b[1]; cin.get(); }
error: cannot convert `int*' to `int (*)[3]' in initialization
We're all Fu##ed aren't we?
Man this is confusing...
We're all Fu##ed aren't we?
Eh, if b is a pointer to a lone array, then printing b[1] results in undefined behaviour, since you are accessing the next array, but it does not exist.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)