Thread: pointers to multidimensional arrays

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Who is to say that memory layout, pointers & all that isn't confusing?
    Hey, everything is confusing in a way.
    Last edited by Elysia; 09-30-2008 at 02:31 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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.

  5. #20
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38

    Fight! Fight!

    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

    Code:
    int (*)[3][2] p = x;
    that (*)... is that a typecast?

    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?

  6. #21
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> 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).
    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
    In short, it's like how int **ptrptr; is a pointer to a pointer, but the second star is replaced by an array specifier.

    >> 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

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Luciferek View Post
    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

    Code:
    int (*)[3][2] p = x;
    that (*)... is that a typecast?
    It's the pointer marker! It says it's a pointer.

    I have issues with pointers! function pointers, array pointers, all pointers.
    You don't necessarily need to use them that often in C++, though :)

    Was Bjarne Strostrup smoking crack when he was developing the syntax?
    You're blaming the wrong individual. It's a relic from C, so blame C!

    Quote Originally Posted by twomers View Post
    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;
    }
    Of yes, you're right.
    Dang. Didn't check that. It's not very often I use pointers to arrays.

    Quote Originally Posted by Elysia View Post
    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.
    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! :)
    Last edited by Elysia; 10-01-2008 at 02:02 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    OK..
    I can live with
    Code:
    int (*p)[3][2] = x;
    It is somewhat consistent with other pointer declarations.

    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?

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Luciferek View Post
    When declaring a pointer to one dimensional array I don't need (* )
    You need parentheses, you just apparently haven't been using them.
    Code:
    int *bob[6]; //an array of 6 pointers-to-int
    int (*george)[6]; //a pointer-to-array-of-int
    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 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-int

  10. #25
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    You need parentheses, you just apparently haven't been using them.
    well no... I do not need parentheses for one dimentional arrays

    Code:
    int main(void){
    int v[3]={3,2,3};
    int *b=v;
    cout<< b[1];
    cin.get();
    }
    But..
    Code:
    int main(void){
    int v[3][2]={3,2,3,3,2,3};
    int *b [2] =v;
    cout<< b[1][2];
    cin.get();
    }
    Gives an error
    We're all Fu##ed aren't we?

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    ...Normally the * will bind with the type (int in this case), rather than the name.
    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-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
    See? See!?! Much easier to read!

    Quote Originally Posted by Luciferek View Post
    well no... I do not need parentheses for one dimentional arrays

    Code:
    int main(void){
    int v[3]={3,2,3};
    int *b=v;
    cout<< b[1];
    cin.get();
    }
    This isn't a pointer to an array.
    Code:
    int main()
    {
        int v[3]={3,2,3};
        int (* b)[3] = v;
        cout<< b[1];
        cin.get();
    }
    But this is!
    And please - drop the void from main. It's not necessary in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    Code:
    int main()
    {
        int v[3]={3,2,3};
        int (* b)[3] = v;
        cout<< b[1];
        cin.get();
    }
    gives me an error
    error: cannot convert `int*' to `int (*)[3]' in initialization
    We're all Fu##ed aren't we?

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah yes, you have to do &v instead of v.
    Dang, I need to test my code more before posting it.

    Code:
    int main()
    {
        int v[3]={3,2,3};
        int (* b)[3] = &v;
        cout<< b[1];
        cin.get();
    }
    There. Now it compiles.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    Man this is confusing...
    We're all Fu##ed aren't we?

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input on arrays and pointers
    By cjohnman in forum C Programming
    Replies: 2
    Last Post: 05-01-2008, 01:57 PM
  2. arrays of pointers to function?????
    By andyzjk in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2005, 09:55 AM
  3. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  4. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM
  5. Hello all and pointers to multi-dimensional arrays
    By Mario in forum C++ Programming
    Replies: 16
    Last Post: 05-17-2002, 08:05 AM