c++arrayscdecl

cdecl clarification required: what is an "array 5?"


Go here: http://cdecl.org/

Input:

char (*arr)[5]

Output:

declare arr as pointer to array 5 of char

What is an "array 5"? Does this simply mean an array with 5 elements?


Solution

  • It is a pointer to an array of 5 elements.

    //Standard array
    char array[5];
    
    //pointer to array
    char (*arr)[5];
    
    //Assign pointer of array to arr
    arr = &array;
    
    //Dereference arr and use it.
    (*arr)[1] = 4;
    

    Pointers and references to arrays are useful for passing arrays to functions, as well as returning them. Do not return local non-static arrays though as their life time ends on return.

    To reference an array you can use this declaration: char (&arr)[5] = array;