DanDooley
DanDooley

Reputation: 35

Implementing a sort on an array of pointers

I'm trying to perform an insertion sort on an array of strings. The array is formatted as an array of pointers to arrays of char.

The array is declared using:

char *wordlist[ARRAY_LEN];

And is passed to the insertion_sort function using:

insertion_sort(wordlist, num_words);

The function for insertion_sort is as below.

void insertion_sort(char **a, int n) {
   char *key;
   int i, p;
   for(p=1; p<n; p++){    /*For every item p in the array apart from the first*/
      key = *a[p];         /*Set key to the value of p*/
      i = p-1;
      while(strcmp(*a[i], key) > 0){ /*For every item to the left of p that is larger*/
         *a[i+1] = *a[i];   /*than it move it 1 to the right*/
         i--;
      }
      *a[i+1] = key;       /*Place key in the vacated leftmost position*/
   }
}

I keep getting errors/warnings relating to pointers upon compile:

word-sort.c: In function ‘insertion_sort’:
word-sort.c:21:11: warning: assignment makes pointer from integer without a cast
word-sort.c:23:7: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast
/usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:7: warning: passing argument 1 of ‘__builtin_strcmp’ makes pointer from integer without a cast
word-sort.c:23:7: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:7: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast
/usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:23:7: warning: passing argument 1 of ‘__builtin_strcmp’ makes pointer from integer without a cast
word-sort.c:23:7: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:7: warning: passing argument 1 of ‘__builtin_strcmp’ makes pointer from integer without a cast
word-sort.c:23:7: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:7: warning: passing argument 1 of ‘__builtin_strcmp’ makes pointer from integer without a cast
word-sort.c:23:7: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:27:15: warning: assignment makes integer from pointer without a cast

Any idea as to where I'm going wrong would be great.

Upvotes: 0

Views: 135

Answers (1)

templatetypedef
templatetypedef

Reputation: 372794

Take a look at this code:

key = *a[p];

Here, key is a char* and a is a char**. This means that a[p] is a char*, so *a[p] is a char. This causes a warning - you're trying to convert a char to a char*, which definitely is worrysome. To fix this, try removing the star here.

You have similar mistakes throughout your code where you're putting extraneous stars in, meaning that you're passing around chars rather than char*s. This explains pretty much all of the warnings you're getting. Try fixing this and see if that resolves the warnings.

Hope this helps!

Upvotes: 2

Related Questions