1

As part of a small C program I've written, I have an insertion sort function that inserts a given string into a given array in its sorted location. I've gotten the function to work, but I'm wondering why I need to specify the second dimension for the array in the function definition to keep from getting a compilation time error. In my function, if I leave out the LONGEST_WORD macro, I get an "array has incomplete element type 'char []'" error when compiling. Everything runs smoothly when I keep it in. Could someone please explain why? Thank you!

#include <string.h>

int insertInOrder(char array[][LONGEST_WORD], char* word, int wordCount) {
    int i, j, location = wordCount;

    /* finds index of location to insert word; */
    for (i = 0; i < wordCount; i++) {
        if (strcmp(word, array[i]) <= 0) {
            location = i;
            break;
        }
    }

    /* makes space for new word to be inserted, shifting all words greater than word to the right by one */
    for (j = wordCount; j > location; j--) strcpy(array[j], array[j-1]);
    
    strcpy(array[location], word); /* copies new word to its location */

    return 0;
}
  • 2
    In order to know the distance from array[0] to array[1] it needs to know the size of array[0]. etc. – QuentinUK Mar 08 '22 at 01:37
  • When an array is used as a parameter, it will degenerate into a pointer, char array[][LONGEST_WORD] is equivalent to char ( * array)[LONGEST_WORD], the compiler needs to know the specific type of the pointer, char ( * array)[LONGEST_WORD] and char ( * array) [LONGEST_WORD-1] are two different types. If you are trying to use a array of string, which may be declare as char * [] instead of char[][LONGEST_WORD]. – TianZerL Mar 08 '22 at 01:52
  • duplicates: [Why is it allowed to omit the first dimension, but not the other dimensions when declaring a multi-dimensional array?](https://stackoverflow.com/q/33328307/995714), [Why do we need to specify the column size when passing a 2D array as a parameter?](https://stackoverflow.com/q/12813494/995714) – phuclv Mar 08 '22 at 03:41

2 Answers2

0

C is concerned with the actual memory size of your array. char array[][LONGEST_WORD] is an array of arrays of char that are LONGEST_WORD in length.

Knowing this, the compiler knows that array[n+1] is LONGEST_WORD bytes past the address of array[n].

If you hadn't specified this, it doesn't know how to address elements in that array.

Chris
  • 26,361
  • 5
  • 21
  • 42
0

A "2D array" is nothing more than a 1D array of 1D arrays.

To find the address of an element in a 1D array the compiler needs to know the address of the array and the size of an element (e.g. for uint32_t myArray[10];, to find the address of myArray[i] C does something like address = (void *)myArray + i * sizeof(uint32_t)).

To find the address of an element in a 1D array (of 1D arrays) the compiler needs to know the address and the size of an element, and the size of the outer array's elements is the size of a whole inner 1D array which depends on its type and how many elements the inner array has.

E.g. for uint32_t myArray[20][10];, to find the address of myArray[i] C does something like address = (void *)myArray + i * sizeof(uint32_t innerArray[10])); and to find the address of myArray[i][j] C does something like outerAddress = (void *)myArray + i * sizeof(uint32_t innerArray[10]) and then does innerAddress = outerAddress + j * sizeof(uint32_t). Of course this can (and normally would) be simplified into innerAddress = (void *)myArray + (i * 10 + j) * sizeof(uint32_t).

if I leave out the LONGEST_WORD macro, I get an "array has incomplete element type"

Yes. Essentially, the outer 1D array is a 1D array of "incomplete element type" elements; and not a 1D array of "1D array of LONGEST_WORD chars" elements.

Brendan
  • 35,656
  • 2
  • 39
  • 66