![]() ![]() Since the expression arr is equivalent to *(arr + x), the pointer is implicitly dereferenced. Note that in your function, you would subscript arr as normal: arr =. Since int *a and int a are synonymous in a function parameter declaration, it follows that int (*a) is synonymous with int a, so you could write the above as void box_sort(int arr, int x, int y)Īlthough I personally prefer the pointer notation, as it more accurately reflects what's going on. What this means in the context of your code is that in your call to box_sort, the type of the expression boxes is implicitly converted from M-element array of N-element array of int to pointer to N-element array of int, or int (*), so your function should be expecting parameter types like: void box_sort(int (*arr), int x, int y) The exceptions to this rule are when the array expression is an operand of the sizeof or address-of ( &) operators, or if the array expression is a string literal being used to initialize another array in a declaration. When an array expression appears in most contexts, its type is implicitly converted from "N-element array of T" to "pointer to T", and its value is set to the address of the first element in the array. But the core of the question here is whether a two-dimensional array and a double pointer are the same thing. Note: As others have noted, int isn't a real type only one of the sizes can be unspecified. You could construct the second one like this: int **a = malloc(2 * sizeof(int*)) dynamically allocated with nested mallocs) Here's what they'd look like in memory for a 2x2 array: int a: But for a two-dimensional array, these are very different methods of storing. You can use int and int* interchangeably in many cases, in particular in cases like this because the array decays to a pointer to the first element when you pass it into a function. The answer is, int (see note at the bottom) and int** are definitely not the same type. I know there was a question almost exactly like this a couple days ago.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |