できるよ:
void sortnames(char **names, int low, int high, int num_names, int *sizes)
ここでは、最初のパラメーターで名前の配列を渡します。最初の座標のサイズは である必要がnum_namesあるため、セグメンテーション違反の問題は発生しません。次に、最後のパラメータに各文字列の長さの配列を渡すことができます。この最後のパラメーターも sizenum_namesである必要がありsizes[i]、string の長さを示しますnames[i]。
編集: セグメンテーション違反は、C で触れることが許可されていないメモリにアクセスするたびに発生するエラーです。通常、範囲外の配列要素にアクセスしているときに発生します。これを回避するには、 への適切な呼び出しを使用して、配列に十分なスペースを割り当てる必要がありますmalloc。したがって、たとえば、sortnames関数を呼び出すには、多かれ少なかれ次のように文字列の配列の前に宣言する必要があります (実行しているコンテキストがわからないため、多かれ少なかれ言います):
int num_names // This is the number of names you want to read
// Populate the variable num_names
// ...
char **to_sort = malloc(num_names * sizeof(char *));
int i;
for(i = 0; i < num_names; ++ i)
{
to_sort[i] = malloc(max_name_size); // Here max_name_size is a static value
// with the size of the longest string
// you are willing to accept. This is to
// avoid you some troublesome reallocation
}
// Populate the array with your strings using expressions like
// to_sort[i] = string_value;
//...
int *sizes = malloc(num_names * sizeof(int));
for(i = 0; i < num_names; ++ i)
{
sizes[i] = strlen(to_sort[i]);
}
sortnames(to_sort, 0, num_names, sizes);
また、 への呼び出しでセグメンテーション エラーが発生しないように、文字列を null で終了することを忘れないでくださいstrlen。