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.
strcpyis the wrong way to go here. The OP should just reassign the pointers.