0

I am trying to send this array to the function sortByLength.

The function prototype is

sortByLength(char *words[],int * lengths, const int size);

Here is the rest of my code up to where I try to pass it to the function:

#include <stdio.h>
#include "task2.h"
#define SIZE 3

int main(void)
{
    int i;
    int lengths[SIZE] = {3, 4, 6};
    char words[][20] = {"Sun", "Rain", "Cloudy"};

    sortByLength(words[][20],lengths,SIZE);

    return 0;
}
6
  • 4
    What's your question? Commented Mar 24, 2015 at 5:00
  • Why not sortByLength(words, lengths, SIZE); Commented Mar 24, 2015 at 5:01
  • @kiks73 It will not work. Because he declared that as a 2-D array. But he getting that as pointer to pointers. Commented Mar 24, 2015 at 5:06
  • @Karthikeyan.R.S; Its not array of pointers but pointer to pointer. Commented Mar 24, 2015 at 5:07
  • @haccks so 'char words[][20]' is a pointer that points to the pointer that points to the first element "Sun"? Commented Mar 24, 2015 at 14:34

1 Answer 1

2

The prototype

sortByLength(char *words[],int * lengths, const int size);  

is equivalent to

sortByLength(char **words,int * lengths, const int size);  

words[][20] will converted to pointer to array of 20 chars when passed to a function. char ** and char (*)[20] are incompatible types. Change function prototype to

sortByLength(char (*words)[20], int *lengths, const int size); 
Sign up to request clarification or add additional context in comments.

1 Comment

or use sortByLength(words,lengths,SIZE); instead of sortByLength(words[][20] ,lengths,SIZE);.

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.