【问题标题】:Incompatible Pointer Type passing Argument in C?不兼容的指针类型在 C 中传递参数?
【发布时间】:2014-11-11 06:53:41
【问题描述】:

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

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;
}

}

【问题讨论】:

    标签: c pointers recursion merge mergesort


    【解决方案1】:

    行内:

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

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

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

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

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

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

    【讨论】:

      【解决方案2】:

      mergeSort()的签名

      void mergeSort(listNode ** listPtr, listNodeCompareFcn compare)
      

      merge()的签名

      listNode * merge(listNode * l1, listNode * l2, listNodeCompareFcn compare)
      

      这意味着merge() 的第一个参数应该是listNode * 类型,而在你的情况下,

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

      它的void

      第二个参数相同,不兼容。

      提示:尝试显示代码对应的行号并检查错误/警告消息。

      【讨论】:

      • 你会建议我做什么来解决这个问题?
      • 我们并没有检查您的代码逻辑,但请注意一件事,您的mergeSort() 函数实际上没有返回任何值。
      • 好吧,也许 merge(head, other, compare) 可能会起作用,虽然我不确定这是我正在寻找实现 mergeSort 的递归语句
      【解决方案3】:

      您可以将&amp;header and &amp;other 传递给mergesort 函数调用。它只会避免编译器警告,但您需要查看排序的功能。 例如,您可以像 mergesort(&amp;head,compare)mergesort(&amp;other,compare) 一样调用您的函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-27
        • 2017-12-11
        • 2013-12-25
        • 1970-01-01
        • 2011-01-10
        • 2020-06-07
        相关资源
        最近更新 更多