Double Pointer & Two Dimension Array ....

This is a discussion on Double Pointer & Two Dimension Array .... within the C Programming forums, part of the General Programming Boards category; In the following code. when i use the name <File as a double pointer> which is actually an array, I ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    Double Pointer & Two Dimension Array ....

    In the following code.
    when i use the name <File as a double pointer> which is actually an array, I am able to extract the contents of the array . However when i create a double pointer Array & assign it to the File, Using Arraypp I am not able to extract the contents of the variable, Why is it so?
    Any help is appreciated.

    Code:
    const char File[][15]  =    {
                                   "New", 
                                   "Open",
                                   "Close",
                                   "save"
                                  };      
    
    
    void  Demo_Pointers_to_Multi_Array(void){
           // This is still incomplete have to understand... 
           const char **Arraypp= (const char **)&File[0][0];
           
          
           printf ("\n %u",Arraypp);
           printf("\n %u",File);
           printf("\n %c ",*(*(File+ 0) +0));
           printf("\n %c ",*(*(Arraypp + 0) +0));
          while(1);
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I too ran this code and this is what I've noticed.
    Your assignment here is incorrect-
    const char **Arraypp= (const char **)&File[0][0];
    &File[0][0] is an address where a character resides and what you're doing is making a pointer to char* and pointing it to &File[0][0], which is logically incorrect since &File[0][0] contains a char not a char*. So while File and Arraypp both have the same address but to get the value at the address &File[0][0], you should do *(Arraypp+0) not *(*(Arraypp + 0) +0). When you do the latter, the code tries to access random memory which results in segmentation fault. The whole thing can be corrected simply by not making Arraypp a pointer to char* but a pointer to char, which can be done as this
    const char *Arraypp= (const char *)&File[0][0];
    I hope I'm understood. Better if someone can throw more light on this one.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The original File is a two-dimensional array; in this case, that means you have 4*15=60 consecutive characters of storage. You cast that base address when you assign Arraypp. Hence since Arraypp is a double pointer, *Arraypp is still a pointer-to-char, so the data at that address -- the first four characters "New\0" -- is treated, not as four characters, but as the value of a pointer-to-char. It is unlikely that you own the memory address 0x0077654e which that (probably) translates to.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Isn't this logically incorrect to do this:
    const char **Arraypp= (const char **)&File[0][0];
    Arraypp has the address of a pointer to char which is not the case here. Here Arraypp has the address which has a char stored in it. So, it's illogical.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, it's not always "logically incorrect" to fiddle with pointers in that way (i.e., if people want to fiddle with the bytes of a float for whatever reason, they cast a float* to a char* and voilá). But in this case things will not end well, that is true -- the data will not lend itself to being interpreted as char* instead of as char.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Hi BEN10,
    Thank you for the reply. I misunderstood the concept, your explanation made it clear. .

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Arrays and Pointers

    And your declaration/assignment should have been
    Code:
    const char (*Arrayp)[15] = File;
    Just the same as if you wanted to pass File to a function, eg.
    Code:
    void myfunc ( const char (*Arrayp)[15] ) {
      if ( Arrayp[0][0] == 'N' ) {
      }
    }
    
    //
    
    int main ( ) {
      myfunc( File );
      return 0;
    }
    No casting, no tricky pointer arithmetic.

    The same syntax which worked on the real array will still work on the pointer to it - providing you get the declaration right to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Hi Salem,
    Thanks finally it worked....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating a double pointer as an array
    By sharrakor in forum C Programming
    Replies: 3
    Last Post: 04-16-2009, 07:02 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  5. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21