Arrays with pointers

This is a discussion on Arrays with pointers within the C Programming forums, part of the General Programming Boards category; Hi guys Lets look at an example: Code: char *str[]={"hi", "all"}; This code creates an array of pointers to char, ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Arrays with pointers

    Hi guys

    Lets look at an example:

    Code:
    char *str[]={"hi", "all"};
    This code creates an array of pointers to char, and makes the pointers point to the character arrays "hi" and "all", respectively. Now, my question is regarding the following code:

    Code:
    int intarr[2][2]={{1,2} , {3,4}};
    int *intptr[]=intarr;
    As you may know by now, the above code is completely wrong. My question is: Why is it wrong? Because in the first example with strings, the array of pointers to char point to character-arrays. In the second example, the pointers to integer point to integer-arrays.

    Why is it that in the integer-case, I must specify that the pointers point to arrays of length 2 (i.e. int (*intptr)[2] is correct), while I do not have to do that in the string-case?

    Best,
    Niles.
    Last edited by Niels_M; 07-30-2010 at 04:35 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe Salem answered this just recently. IIRC, he said the name of the array is a pointer to the first element of the array - you know that.

    The address of the name of the array, is the value you need for a pointer to the whole array.

    It's WAAAYYYY late, so I'm not sure if that's right, but try it.

    This works, but isn't quite what I thought would work:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
      int i; 
    
      int intarray[2][2]={{1,2}, {3,4}};
      int *pint=*intarray;
    
      printf("\n\nN1=%d, N2=%d, N3=%d", *(pint+0), *(pint+1), *(pint+2) );
    
      printf("\n\n\t\t\t     press enter when ready");
    
      i = getchar();
      return 0;
    }
    Last edited by Adak; 07-30-2010 at 05:02 AM.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    22
    hi Niels_M,

    Its because the name of the 2*2 matrix intarr is a pointer to an array of two pointers each of which point to a row of the matrix.....

    So that is why you have to inform the compiler like (*intptr)[2])....

    Hope this helps
    Regards
    C_Enthusiast

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    But intarr = &intarr[0] = &intarr[0][0]?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a better example, from Elysia's suggestion in another thread:

    Code:
    #include <stdio.h>
    
    
    int main() {
      int i, j, len, n; 
    
      int Num[2][2]={{1,2}, {3,4}};
      int (*pint)[2]=Num;
       
    
      printf("\n\nN1=%d, N2=%d, N3=%d, N4=%d", pint[0][0], pint[0][1], \
      pint[1][0], pint[1][1] );
    
      printf("\n\n\t\t\t     press enter when ready");
    
      i = getchar(); ++i;
      return 0;
    }
    I like this one. Makes sense to me.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    As you may know by now, the above code is completely wrong. My question is: Why is it wrong? Because in the first example with strings, the array of pointers to char point to character-arrays. In the second example, the pointers to integer point to integer-arrays.
    No, in the first example, You have an array of pointer to char.
    The type of array element is pointer to char.
    Last edited by Bayint Naung; 07-30-2010 at 06:31 AM.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Bayint Naung View Post
    No, in the first example, You have an array of pointer to char.
    The type of array element is pointer to char.
    That is also what my original post states.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Take string literals as anonymous char array which you can't modify and have static scope.

    Code:
    static char _0[ ]  = "hi";
    static char _1[ ]  = "all";
    
    char *str[]={ _0 , _1 };
    >>That is also what my original post states.
    You states the array of pointers to char point to character-arrays.
    The type of str is array! It's not pointer! It's array initialization.
    Last edited by Bayint Naung; 07-30-2010 at 06:42 AM.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Bayint Naung View Post
    Take string literals as anonymous char array which you can't modify and have static scope.

    Code:
    static char _0[ ]  = "hi";
    static char _1[ ]  = "all";
    
    char *str[]={ _0 , _1 };
    I will. But I think we are all losing focus here with respect to the OP. I'll just quote the question:


    Why is it that in the integer-case, I must specify that the pointers point to arrays of length 2 (i.e. int (*intptr)[2] is correct), while I do not have to do that in the string-case?

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Actually you have answered you question in your question itself.

    char *str[]={"hi", "all"};
    This code creates an array of pointers to char, and makes the pointers point to the character arrays "hi" and "all", respectively. Now, my question is regarding the following code:
    Code:
    int intarr[2][2]={{1,2} , {3,4}};  
    int *intptr[]=intarr;                  // created an array of pointers to int :
                                       that's fine but does the pointers point to int array ?
       // int *intptr[ ] = { intarr[0] , intarr[1] };  // to be equalivent to string analogy
    Are you doing the same thing here?

    Also if you have
    Code:
     char char_arr[2][100] = {    { 'h','i',\0'}  , { 'a', 'l', 'l','\0'}  };
     char *char_ptr[] = char_arr;           // will it work?
     or char (*char_ptr)[2] = char_arr;   // or this ?
    Last edited by Bayint Naung; 07-30-2010 at 07:07 AM.

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Yes, I believe that (obviously I am wrong). Because the name "intarr" is the address of the first element in "intarr", which is (1,2). This has the same address as the element 1.

  12. #12
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The difference is the type. If you do pointer arithmetic.
    Try the values of :
    intarr + 1
    &intarr[0] + 1
    &intarr[0][0] + 1

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Bayint Naung View Post
    The difference is the type. If you do pointer arithmetic.
    Try the values of :
    intarr + 1
    &intarr[0] + 1
    &intarr[0][0] + 1
    Hmm,

    intarr + 1: address of array 2
    &intarr[0] + 1: address of row 2
    &intarr[0][0] + 2: address of first element in row 2

    Am I correct?

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Ok, I feel like this thread got de-railed totally. I really appreciate all the input so far, but it does not seem to be answers to my OP.

    I still don't feel like I have gotten a clear answer, so if anybody would help, I would appreciate it very much.

  15. #15
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Why is it that in the integer-case, I must specify that the pointers point to arrays of length 2 (i.e. int (*intptr)[2] is correct), while I do not have to do that in the string-case?
    Because your two examples are not the same.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with arrays, pointers, and structs.
    By RexInTheCity in forum C Programming
    Replies: 5
    Last Post: 03-29-2010, 03:30 PM
  2. Pointers As 2D arrays
    By TieFighter in forum C Programming
    Replies: 29
    Last Post: 03-22-2010, 06:46 AM
  3. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 06:28 AM
  4. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  5. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM

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