0

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.

3
  • You cant copy an array by this way key = *a[p]; . Use strcpy() instead. Commented Jul 22, 2014 at 0:20
  • 1
    @Abend strcpy is the wrong way to go here. The OP should just reassign the pointers. Commented Jul 22, 2014 at 0:22
  • This code has a lot of problems. Correcting the compiler errors is only the first step. You should start with something simpler, like a function that can swap two strings, before you attempt a full insertion sort. Commented Jul 22, 2014 at 0:28

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.