CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Talking 2d array pointer

    What's wrong with this?

    Code:
        double  coslu[8][] = 0;
    devstudio complains about a missing subscript or some nonsense.
    How do I fix it.

    Sensible suggestions only please

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: 2d array pointer

    The array needs to have a fixed size. For instance:
    Code:
    double  coslu[8][8] = {0.0}; //initialize all elements to 0.0
    Laitinen

  3. #3
    Join Date
    Sep 2007
    Posts
    8

    Re: 2d array pointer

    if youre going to allocate that 2-d array later just do

    double coslu** = 0;

    if youre trying to initialize all values to 0 then first off you need definite bounds for the array in both dimensions and then you have to loop through and set them all to 0 manually

  4. #4
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Re: 2d array pointer

    Quote Originally Posted by laitinen
    The array needs to have a fixed size. For instance:
    Code:
    double  coslu[8][8] = {0.0}; //initialize all elements to 0.0
    Laitinen
    I'm not defining an array.

  5. #5
    Join Date
    Sep 2007
    Posts
    8

    Re: 2d array pointer

    Quote Originally Posted by c94wjpn
    I'm not defining an array.

    ok mind telling us what you're trying to do?

  6. #6
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Re: 2d array pointer

    Quote Originally Posted by Ultimate Hammer
    if youre going to allocate that 2-d array later just do

    double coslu** = 0; /// error stars in the wrong place
    you are defining a pointer to an array of pointers.
    From looking at the memory blocks in a debugger, it is clear that

    double coslu1[8][8];

    does not create an array of pointers.

    hence

    double** p = coslu1;

    will not succeed.

  7. #7
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Talking Re: 2d array pointer

    Quote Originally Posted by Ultimate Hammer
    ok mind telling us what you're trying to do?
    Trying to define a pointer to a 2d array which has a stride of 8.
    If I know that my doubles are in a consecutive memory block at location p, and the stride is 8, then the notations

    p[y][x]
    and
    p[y*8 + x]

    should be equivalent, and hence I should be able to define

    double * q = 0; ///ok

    also

    double q[][8] = 0; /// not ok.

  8. #8
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: 2d array pointer

    Quote Originally Posted by c94wjpn
    Trying to define a pointer to a 2d array which has a stride of 8.
    If I know that my doubles are in a consecutive memory block at location p, and the stride is 8, then the notations

    p[y][x]
    and
    p[y*8 + x]

    should be equivalent, and hence I should be able to define

    double * q = 0; ///ok

    also

    double q[][8] = 0; /// not ok.
    What are you trying to do by assigning it to 0?

    An array is not just a pointer, it occupies a space on the stack. So it cannot (or should not) be NULLed, if that's what you're trying to do. If you're trying to initialize all elements of the array to 0, how is the compiler supposed to know how many elements there are if you don't explicitly supply a second dimension?
    - Alon

  9. #9
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Re: 2d array pointer

    Quote Originally Posted by Hermit
    What are you trying to do by assigning it to 0?

    An array is not just a pointer, it occupies a space on the stack. So it cannot (or should not) be NULLed, if that's what you're trying to do. If you're trying to initialize all elements of the array to 0, how is the compiler supposed to know how many elements there are if you don't explicitly supply a second dimension?
    engage your brain please.

    I can set a pointer to NULL if I want to - I'm not accessing it yet.

    I have explicitly supplied a second dimension :

    double q[][8] = 0; /// not ok. THE 8 IS THE SECOND DIMENSION.

    sensible responses only
    Last edited by c94wjpn; October 23rd, 2007 at 11:16 AM.

  10. #10
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: 2d array pointer

    Quote Originally Posted by c94wjpn
    engage your brain please.

    I can set a pointer to NULL if I want to - I'm not accessing it yet.

    I have explicitly supplied a second dimension :

    double q[][8] = 0; /// not ok. THE 8 IS THE SECOND DIMENSION.

    sensible responses only
    I meant both dimensions. If one dimension is omitted, then the compiler at least needs to be able to deduce that dimension from the initialization list. For example:
    Code:
    double q[][8] = { {0.0}, {0.0}, {0.0} }; // okay, first dimension is 3
    This is only relevant if you're actually aiming at initialization. I don't know what to suggest if you're determined to somehow NULL an array on the stack. It can't be done, and there's no reason to do it.

    And please, drop the condescending tone. Your question is vague at best and if you'd like a sensible answer you'd do best to be a little cooperative.
    Last edited by Hermit; October 23rd, 2007 at 12:03 PM.
    - Alon

  11. #11
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Wink Re: 2d array pointer

    Quote Originally Posted by Hermit
    I meant both dimensions. If one dimension is omitted, then the compiler at least needs to be able to deduce that dimension from the initialization list. For example:
    Code:
    double q[][8] = { {0.0}, {0.0}, {0.0} }; // okay, first dimension is 3
    This is only relevant if you're actually aiming at initialization. I don't know what to suggest if you're determined to somehow NULL an array on the stack. It can't be done, and there's no reason to do it.

    And please, drop the condescending tone. Your question is vague at best and if you'd like a sensible answer you'd do best to be a little cooperative.
    well I think I've outgrown codeguru. Maybe I should quit.

    I'm not creating an array. I'm creating a pointer to a memory block.

    I think I shall take my problem elsewhere.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: 2d array pointer

    Quote Originally Posted by c94wjpn
    well I'm not creating an array.
    So why did you post this:
    Code:
     double  coslu[8][] = 0;
    ?

    What is that supposed to mean? Tell us in your own words what that is supposed to do or what you thought it would do, if it isn't trying to create an array.
    I'm creating a pointer to a memory block.
    I see no "*" syntax anywere in the declaration above. So you are not creating a pointer to anything in your initial post. You are doing something with trying to declare an array -- what exactly is still a mystery.
    I think I shall take my problem elsewhere.
    Unless you explain a little better what you're trying to do, expect the same, if not worse responses from whatever site you take your very vague question to.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 23rd, 2007 at 02:03 PM.

  13. #13
    Join Date
    Oct 2007
    Posts
    12

    Re: 2d array pointer

    I think this guy has no idea of what is talking about, that is all. Let him go elsewhere. He probably guilts other people for his ignorance and in his frustration he just pretends to know what is going on, now don't ask me why he made any question in the first place (needs a psicologist's help not a programmer's)
    --------------------------------------------------------
    --------------------------------------------------------
    Some things in life cannot be explained.
    --------------------------------------------------------
    --------------------------------------------------------

  14. #14
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: 2d array pointer

    Are you trying to do something like this?

    Code:
    double  coslu[8][8];
    
    double *p_coslu = &coslu[0][0]; // Pointer to the start of the 8x8 block of doubles.

  15. #15
    Join Date
    Oct 2007
    Posts
    12

    Re: 2d array pointer

    And that would be just like:

    double coslu[8][8];

    double *p_coslu = coslu;

    I'm thinking, guessing from what he wrote on the title he wanted this:

    double * P_coslu = &coslu[8][0]; // 2nd array?
    --------------------------------------------------------
    --------------------------------------------------------
    Some things in life cannot be explained.
    --------------------------------------------------------
    --------------------------------------------------------

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured