[英]2 warnings: 'int *' differs in levels of indirection from 'int **' and different types for formal and actual parameter 3
我寫了一個代碼,它假設刪除 int 數組中的重復元素而不對其進行排序。
對於那個任務,我編寫了一個名為“removeDuplicateElements”的函數,它獲取一個數組及其字符串,並返回一個動態分配的新數組,它實際上是刪除了所有重復元素的原始數組。 該函數還通過引用返回新數組的大小。
我還在我的代碼函數中使用了構建動態數組並打印它的函數。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void printArray(int* arr, int size);
int* buildArray(int size);
int* removeDuplicateElements(int *arr, int size, int *newSize);
void main()
{
int size;
int *newSize;
int *arr;
int *newArr;
printf("please enter a number for the size of array: ");
scanf("%d", &size);
printf("\nenter %d numbers: ", size);
arr = buildArray(size);
printf("\nthe array after removing the duplicate elements is: ");
newArr = removeDuplicateElements(arr, size, &newSize);
size = *newSize;
printArray(newArr, size);
free(newArr);
free(arr);
}
void printArray(int* arr, int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int* buildArray(int size)
{
int i;
int* arr = (int*)malloc(size * sizeof(int));
if (!arr)
{
printf("ERROR! Not enough memory!\n");
exit(1);
}
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
return arr;
}
int* removeDuplicateElements(int *arr, int size, int *newSize)
{
int *newArr;
int count = size, i, j;
for(i=1;i<size;i++)
for(j=0;j<size;j++)
if (arr[i] == arr[j] && i!=j)
{
count--;
break;
}
newArr = (int*)malloc(count * sizeof(int));
count = 1;
newArr[0] = arr[0];
for (i = 1; i < size; i++)
for (j = 0; j < size; j++)
{
if (arr[i] == arr[j] && i!=j)
break;
else if (j == size - 1)
newArr[count++] = arr[i];
}
*newSize = count;
return newArr;
}
現在,當我運行此代碼時 - 我收到以下兩個警告:
warning C4047: 'function': 'int *' differs in levels of indirection from 'int **'
warning C4024: 'removeDuplicateElements': different types for formal and actual parameter 3
我試圖通過遇到類似問題的問題尋求幫助,但我仍然不明白出了什么問題以及為什么我會收到這些警告。 任何幫助,將不勝感激!
您正在為removeDuplicateElements
第三個參數傳遞int**
,但收到int*
。
int *newSize;
newArr = removeDuplicateElements(arr, size, &newSize);
你的原型,
int* removeDuplicateElements(int *arr, int size, int *newSize)
應該
int* removeDuplicateElements(int *arr, int size, int **newSize)
或者您可以使用int*
,如下所示。
int newSize;
newArr = removeDuplicateElements(arr, size, &newSize);
size = newSize;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.