how to understand this typedef

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evenstar
    New Member
    • Jul 2009
    • 30

    how to understand this typedef

    Hi,
    Code:
      typedef const char* CCARRY[10];
            char * p="Hello";
            p = "Merry";
            CCARRY arr1={p};//OK using the type of char * to initialize to type of const char *
            arr1[0]=p;//OK,assignment
            CCARRY arr2[5]={arr1};//1**compile error:cannot convert 'const char**' to 'const char*' in initialization
            CCARRY arr3[5]={*arr1};//3**ok,right
            arr3[5]=*arr1;//2**compile error:incompatible types in assignment of 'const char*' to 'const char* [10]
    I have some question in ** .I think the type of element in arr2 is CCARRY that is const char* [10].But it is const char *,why?
    And ,if the type of element in arr2 is const char* as compiler said in 1**,why in 2** compiler think the type of element in arr2 is const char* [10]?

    PS:I believe the type of element in arr2 is const char* [10].
    My test environment is vs2008 and GCC4.4.0
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    If you define this array:

    Code:
    const char* CCARRY[10];
    then CCARRY is an array of 10 char* that are const.

    The name of the array, CCARRY is the address of element 0. So CCARRY[0] is a const char* making CCARRY the address of a const char*. That is, CCARRY is a const char**.

    In order to have CCARRY be a pointer to a array of 10 const char* you would need to code:

    Code:
    const char* CCARRY[][10];

    Comment

    • evenstar
      New Member
      • Jul 2009
      • 30

      #3
      If const char* CCARRY[10].then CCARRY is an array of 10 char* that are const.And CCARRY is an array name

      But,in my code it is typedef const char* CCARRY[10].And CCARRY is a type name.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Yes but remembeer that all typedef does is let you name your own types.

        So:
        Code:
        CCARRY arr1={p};
        just saves you from writing:
        Code:
        const char* arr1[10];
        CCARRY may have meaning in your program that makes using that name convenient.
        Last edited by Niheel; May 7 '11, 05:30 PM.

        Comment

        Working...