c - 不兼容的指针类型在 C 中传递参数?

标签 c pointers recursion merge mergesort

我正在尝试在链表上实现合并排序。遇到会产生以下错误的问题:

student@wheezyupsec:~/CS305/HW4$ make
gcc -c sort.c
sort.c: In function ‘mergeSortHelper’:
sort.c:70:2: warning: passing argument 1 of ‘mergeSort’ from incompatible pointer type [enabled 
by default]
sort.c:34:6: note: expected ‘struct listNode **’ but argument is of type ‘struct listNode *’
sort.c:70:2: warning: passing argument 1 of ‘mergeSort’ from incompatible pointer type [enabled  
by default]
sort.c:34:6: note: expected ‘struct listNode **’ but argument is of type ‘struct listNode *’
sort.c:70:2: error: invalid use of void expression
sort.c:70:2: error: invalid use of void expression
make: *** [sort.o] Error 1

谁能看到我的错误。它与我进行递归语句的 mergeSortHelper 有关。它需要一个双指针而不是给定的单个指针。但是不知道该怎么做。我把我的 sort.c 代码放在下面。感谢您的帮助。

 /* 
 * sort.c
 * 
 * Author: CS305 STUDENT ADD YOUR NAME HERE.
 * 
 * Description: Contains sorting functions that operate on the linked
 * list implementation for the company entry node found in
 * list.[c,h].
 *
 */

 #include <stdio.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <math.h>

 #include "list.h"
 #include "sort.h"


 // Function prototypes for helper functions used only within this
 // file.  They are declared as static since they should be global
 // function declarations available only to this file.
 static listNode * mergeSortHelper(listNode * head, listNodeCompareFcn compare);
 static listNode * merge(listNode * l1, listNode * l2, listNodeCompareFcn compare);


/* mergeSort()
 *
 * CS305 students should *not* alter this function for HW4.  Nope
 * Don't do it.
 */
void mergeSort(listNode ** listPtr, listNodeCompareFcn compare)
{
// mergeSort() calls mergeSortHelper which performs the actual
// merge sort algorithm.  This function simply points the head
// of the list at the result of the sorting.
*listPtr = mergeSortHelper(*listPtr, compare);
}

// CS305 Students must implement the function stubs below.  To maximize points earned
// on the homework, students should also supply function comment headers as well as
// document the function bodies with useful comments.


/* mergeSortHelper()
*
* CS305 students must implement this function for HW4.
*/
listNode * mergeSortHelper(listNode * head, listNodeCompareFcn compare)
{
listNode * chop = head;
listNode * other = head->next;
int n = count(head);
int i = 0;
while (i<((n/2) - 1))
{
    //advance chop and other
    chop = chop->next;
    other = other->next;
    i++;
}
chop->next = NULL;

//return head;

//return merge(mergesort(head, compare), mergesort(other, compare), compare);

return merge(mergeSort(head, compare), mergeSort(other, compare), compare);

}

/* merge()
* Parameters: 1. l1: the first linked list to be merged.
*             2. l2: the second linked list to be merged.
*
* Description: Merge two sorted linked lists and return the merged
*              list.
*
* CS305 students must implement this function for HW4.
*
*/
listNode * merge(listNode * l1, listNode * l2, listNodeCompareFcn compare)
{
listNode * head;
//Base Case
if(l1 == NULL)
{
    return l2;
}
if(l2 == NULL)
{
    return l1;
}
//recursive case
if(compare(l1, l2))
{
    head = l1;
    head->next=merge(l1->next, l2, compare);
}
else
{
    head = l2;
    head->next = merge(l1, l2->next, compare);
}

return head;

}

/* alphabetCompare()
*
* Given two pointers to listNode, return 1 if the first one's
* companyName is lexicographically less than the second one.
* Otherwise, return 0.
* 
* For example, if l1->companyName is 'aaaa' and l2->companyName is
* 'aaab' then l1->companyName is less than l2->companyName.
* 
* CS305 students must implement this function for HW4.
* 
*/
int alphabetCompare(listNode * l1, listNode * l2)
{
if(strcmp(l1->entryPtr->companyName,l2->entryPtr->companyName)<0)
{
    return 1;
}
else
{
    return 0;
}
}

 /* distanceCompare()
 *
 * Given two pointers to listNode, return 1 if the first one's
 * latitude + longitude place it closer to the University of Portland
 * Bell Tower than the second one.  Otherwise, return 0.
 *
 * CS305 students must implement this function for HW4.
 *
 * For full points, the comparison should be made based on the
 * distance between two points on a sphere.  For 80% credit
 * a simple comparison can be made between two points on a plane.  
 *
 */
int distanceCompare(listNode * l1, listNode * l2)
{
//convert longitude and latitude of bell tower
//coordinates to radians
long upLonRadians = BELL_TOWER_LON/(180/PI);
long upLatRadians = BELL_TOWER_LAT/(180/PI);
//convert radians of bell tower coordinates 
//to spherical coordinates (x,y,z)
long upX = cos(upLatRadians) * cos(upLonRadians);
long upY = cos(upLatRadians) * sin(upLonRadians);
long upZ = sin(upLatRadians);
//convert longitude and latitude of company
//coordinates in l1 and l2 to radians
long comp1LonRadians = (l1->entryPtr->latitude)/(180/PI);
long comp1LatRadians = (l1->entryPtr->longitude)/(180/PI);
long comp2LonRadians = (l2->entryPtr->latitude)/(180/PI);
long comp2LatRadians = (l2->entryPtr->longitude)/(180/PI);
//convert radians of company coordinates 
//to spherical coordinates (x,y,z) for
//both companies
long comp1X = cos(comp1LatRadians) * cos(comp1LonRadians);
long comp1Y = cos(comp1LatRadians) * sin(comp1LonRadians);
long comp1Z = sin(comp1LatRadians);
long comp2X = cos(comp2LatRadians) * cos(comp2LonRadians);
long comp2Y = cos(comp2LatRadians) * sin(comp2LonRadians);
long comp2Z = sin(comp2LatRadians);
//find distance between bell tower and
//company 1 and distance between bell
//tower and company 2.
long dist1 = arccos((upX*comp1X)+(upY*comp1Y)+(upZ*comp1Z))*EARTH_RADIUS;
long dist2 = arccos((upX*comp2X)+(upY*comp2Y)+(upZ*comp2Z))*EARTH_RADIUS;
//compare distances between two companies.
//if company 1 distance is closer than
//company 2, return 1, otherwise return 0.
if(dist1<dist2)
{
    return 1;
}
else
{
    return 0;
}

}

最佳答案

行内:

return merge(mergeSort(head, compare), mergeSort(other, compare), compare);

您正在调用 mergeSort() 的外部接口(interface),它返回 void。您收到错误的部分原因是 mergeSort() 采用 listNode ** 而您传递的是 listNode *,部分原因是它没有' 返回一个值,但您正试图将不存在的值传递给另一个函数。

您可以通过以下方式解决间接问题:

mergeSort(&head, compare);
mergeSort(&other, compare);
return merge(head, other, compare);

但使用内部接口(interface)可能更简单/更好,mergeSortHelper():

return merge(mergeSortHelper(head, compare), mergeSortHelper(other, compare), compare);

关于c - 不兼容的指针类型在 C 中传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26859244/

相关文章:

C 跳过函数的一个命令?

c - 将值关联到相应的文件

c++ - 为什么引用比指针更安全?

c - 无法使指向数组的指针正常工作 - 应用程序崩溃

java - 新手指针 rs.next

java - 两个单链表的非破坏性递归相交

c - qsort 比较全零

c - 结构链表中的元素编号

C 关于PUTCHAR

java - toString() 中的无限递归,如何防止这种情况发生?