+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Junior Member shred2er is on a distinguished road
    Join Date
    Sep 2003
    Posts
    10

    Pointers to Multidimensional Arrays

    Hi there,

    I have a problem that I can't seem to resolve. I know that if we have, say, an integer array A[5], then we can initialise a pointer to an integer as follows:

    int* B = A;

    But I can't seem to figure out what kind of pointer can be initialised to the name of a multidimensional array. For instance, if I have A[5][5], and I want to initialise B to A:

    X B = A;

    what should X be?

    Thanks a lot for your help.

    Sincerely,

    shred2er.

  2. #2
    Junior Member vanika is on a distinguished road
    Join Date
    Sep 2003
    Posts
    1
    well ,
    it should be
    int *B=A

    this will store the base address that is the starting address of array A ,the 2-d array are stored as linear array internally

  3. #3
    Junior Member shred2er is on a distinguished road
    Join Date
    Sep 2003
    Posts
    10
    Hi vanika,

    Thanks for the response. I had already tried that before posting, but unfortunately it does not work.

    In my program I have defined an array A[3][3]. Then I tried to initialise a pointer to an integer B as follows: int* B = A. The compile-time error is:

    MatrixOperations.C: In function `int main (int, char **)':
    MatrixOperations.C:50: cannot convert `int (*)[3]' to `int *' in
    initialization
    I have also tried int** B = A. There is noe that does not give any compile-time error: int* B = A[5]. But dereferencing B does not produce the desired results.

    But I thank you for your trouble. Perhaps you have some other ideas as well?

    Sincerely,

    shred2er.

  4. #4
    AntiOnline Senior Member souleman souleman souleman souleman souleman souleman souleman souleman souleman souleman souleman souleman's Avatar
    Join Date
    Oct 2001
    Location
    Flint, MI
    Posts
    2,884
    int (*B)[3] = A


    if I remember correctly, its been a long time
    \"Ignorance is bliss....
    but only for your enemy\"
    -- souleman

  5. #5
    Member Q-bel will become famous soon enough
    Join Date
    Sep 2003
    Posts
    76
    Try specifying to the pointer that it has to point to the first element in the array.


    For example
    int A[2][2];
    int*C;
    C=&A[0][0];


    tell me if it gives you any errors
    I Speak in frequencies even dogs have trouble hearing


  6. #6
    Junior Member shred2er is on a distinguished road
    Join Date
    Sep 2003
    Posts
    10
    Originally posted here by Q-bel
    Try specifying to the pointer that it has to point to the first element in the array.


    For example
    int A[2][2];
    int*C;
    C=&A[0][0];


    tell me if it gives you any errors
    Woohoo! Thanks, Q-bel! It works!

  7. #7
    Member Q-bel will become famous soon enough
    Join Date
    Sep 2003
    Posts
    76
    No problem man just as they've been helping me learn here I gotta try and help out at least in what I can jeje.

    I Speak in frequencies even dogs have trouble hearing


Bookmarks

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides