Thread: Arrays with pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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.

  10. #10
    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

  11. #11
    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?

  12. #12
    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.

  13. #13
    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.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Niels_M View Post
    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.
    They're still discussing your original issue.
    I am not 100% sure of what the standard says, but so far as I see, there is simply no implicit conversion from int[][] to int*[].
    I believe that the 1D array is simply a special case as it decays to a mere pointer. In the 2D case, it treats the right-hand side as an array (which does not decay).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    A: The rule (see question 6.3) by which arrays decay into pointers is not applied recursively. (Once the rule has been applied once, the result is a pointer to which the rule no longer applies.) An array of arrays (i.e. a two-dimensional array in C) decays into a pointer to an array, not a pointer to a pointer. Pointers to arrays can be confusing, and must be treated carefully; see also question 6.13.
    c-faq

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, 07: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, 04:49 PM