pointer to array of pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aveeakki
    New Member
    • Dec 2015
    • 4

    pointer to array of pointers

    Can anyone explain how to access the 1st element of 1st array in array of pointers?

    suppose i have an array of 3 pointers, i define another pointer to this array of pointer. how do i access 1st element of 1st pointer in that array?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    An array of pointers is:

    Code:
    int* ptr[3];
    so the 3 int pointers are ptr[0], ptr[1], and ptr[2].

    OK. Now a pointer to this array is a pointer to an array of pointers. For all arrays, the name of the array is the ADDRESS of element 0. So you need:

    Code:
    int** val = ptr;
    So val is the address of ptr[0]. Therefore, *val is ptr[0]. Therefore, **val is the int pointed at by ptr[0].

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Pointer Arithmetic
      When adding a pointer val and an in integer i, it is assumed that the pointer points somewhere into an array, call that the current position. The effect of adding integer i is to reference the ith object in the array relative to the current position.
      Code:
      *(val+0) refers to the same location as ptr[0]
      *(val+1) refers to the same location as ptr[1]
      *(val+2) refers to the same location as ptr[2]
      Array Subscripting
      Array subscripting is defined in terms of pointer arithmetic: a[i] is the same as *(a+i), thus:
      Code:
      val[0] is same as *(val+0) is same as ptr[0]
      val[1] is same as *(val+1) is same as ptr[1]
      val[2] is same as *(val+2) is same as ptr[2]

      Comment

      • aveeakki
        New Member
        • Dec 2015
        • 4

        #4
        these are the pointers i will be using to read my inputs stored in diff files.


        int32 MIXER_R_InPntr = 0 ;
        int32 MIXER_C_InPntr = 0 ;
        int32 MIXER_Sw_InPntr = 0 ;
        int32 MIXER_MIXERFL_I nPntr = 0 ;
        int32 MIXER_MIXERFR_I nPntr = 0 ;
        int32 MIXER_MIXERRL_I nPntr = 0 ;
        int32 MIXER_MIXERRR_I nPntr = 0 ;

        int32 MIXER_IN_PNTR[6] ={0}; //MIXER_MIXERFL_I nPntr, MIXER_MIXERFR_I nPntr, MIXER_MIXERRL_I nPntr, MIXER_MIXERRR_I nPntr, MIXER_C_InPntr, MIXER_Sw_InPntr };


        int32 *MIXER_IN_PNTR[6];
        int32** IN_PNTR = MIXER_IN_PNTR;


        when i am building i am getting error message
        error C2040: 'MIXER_IN_PNTR' : 'int32 *[6]' differs in levels of indirection from 'int32 [6]'.



        Please Help.

        Comment

        • aveeakki
          New Member
          • Dec 2015
          • 4

          #5
          my_array[6] = {FL, FR, RL, RR, RL, C, Sw};
          these array elements are used to read samples from respective files.

          my_array[0] points to FL, now i want to point to 1st sample (considering the files from which inputs are read consists of sample values) of FL.

          How do i obtain that using pointers?

          Please Help.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You have:

            Code:
            int32 MIXER_IN_PNTR[6] ={0};
            and also:
            Code:
            int32 *MIXER_IN_PNTR[6];
            You aren't allowed to have 2 arrays with the same name.

            Which of these is correct?

            Comment

            • aveeakki
              New Member
              • Dec 2015
              • 4

              #7
              int32 MIXER_IN_PNTR[6] = {0}; is correct

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                MIXER_IN_PNTR is defined as an array of integers, not an array of pointers.

                To make it an array of pointers:
                int32 *MIXER_IN_PNTR[6];

                To initialize it to point at the previously defined integers:
                int32 *MIXER_IN_PNTR[6] = { &MIXER_MIXERFL_ InPtr, ... };
                In this case, you can change the integers pointed to by array elements and you can also change the array elements to point at different integers.

                To initialize it so that you can still change the integers pointed to by array elements but can't change the array elements themselves:
                int32 * const MIXER_IN_PNTR[6] = { &MIXER_MIXERFL_ InPtr, ... };

                Comment

                Working...