English 中文(简体)
关于特征阵列点和次要点以及q功能的问题
原标题:Questions about character array pointers and secondary pointers and the cmp function in the qsort function
//Sort character array lexicographically (main code):
char* words[MAX_SIZE];
qsort(words, len, sizeof(char*), cmp);
int cmp(const void* w1, const void* w2) {
    char** str1 = w1;
    char** str2 = w2;
    return strcmp(*str1, *str2);
    //return strcmp(w1, w2);
}

Why does the cmp function have to define a secondary pointer to output normally? Why can t it directly return strcmp(w1, w2);? Please! Help!

My understanding is that w1 and w2 are pointers (words) pointing to the string head pointer (words[i]), is the pointer of the pointer, and needs to be dereferenced to compare. Is it right?

最佳回答

对<代码>qsort的比较功能通过两名成员的地址比较。 由于该阵列含有“<代码>char *的内容,这些成员的地址为char **

www.un.org/chinese/ga/president

问题回答

这里的直观答案,我可以说:

“entergraph

What you know vs what qsort() knows

You know that your array is an array of char*.

qsort() only knows it has an array of bytes. That is why you also have to tell it how many bytes each element takes (in our example we are pretending a char* is 4 bytes, though on modern hardware it is almost always 8), in addition to the number of elements in the array.

qsort(>> 然后打电话给你的比较功能时,not知道它有一系列的char*。 它有各种各样的 by。

因此,它也不能直接通过该要素(char*)。 因此,该要素要有一个点。 因此,第12号星号的点子是<条码>第4条要素(指数=3)的点子。

注意特性:char**

仅凭的简单明细表(<>/code>>)而不知(或谨慎)点码。

因此,比较职能只是指点,在职能范围内,你必须将该点变成正确点。

The comparison function (strings)

如果您的阵列是一系列扼杀(char*s),那么:

int pstrcmp( void* ptr_to_a, void* ptr_to_b )
{
  // Convert the argument to the correct type of pointer
  char** ptr_to_string_a = ptr_to_a;
  char** ptr_to_string_b = ptr_to_b;

  // Access the pointed-to array element
  char* a = *ptr_to_string_a;
  char* b = *ptr_to_string_b;

  return strcmp( a, b );
}

当然,我们可以将这一点简单地缩小到:

int pstrcmp( void* ptr_to_a, void* ptr_to_b )
{
  return strcmp( *(char**)ptr_to_a, *(char**)ptr_to_b );
}

The comparison function (integers)

相比之下,如果我们的阵列是一系列的愤怒:

<代码>int integers [5];

然后,qsort(>,仍然不知道这些要素是什么,并将给你一个要点的协调人。 计算单位仅为<代码>int*。

int pcmpint( void* ptr_to_a, void* ptr_to_b )
{
  // Convert the argument to the correct type of pointer
  int* ptr_to_int_a = ptr_to_a;
  int* ptr_to_int_b = ptr_to_b;

  // Access the pointed-to array element
  int a = *ptr_to_int_a;
  int b = *ptr_to_int_b;

  if (a < b) return -1;
  if (b < a) return  1;
  return 0;
}

或更简明扼要:

int pcmpint( void* ptr_to_a, void* ptr_to_b )
{
  if (*(int*)ptr_to_a < *(int*)ptr_to_b) return -1;
  if (*(int*)ptr_to_b < *(int*)ptr_to_a) return  1;
  return 0;
}

简单类型,如ger,通常会看以下,因为更容易阅读。 (汇编者可为所有这些变量制定相同的代码。)

int pcmpint( void* ptr_to_a, void* ptr_to_b )
{
  int a = *(int*)ptr_to_a;
  int b = *(int*)ptr_to_b;

  if (a < b) return -1;
  if (b < a) return  1;
  return 0;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签