簡體   English   中英

警告:格式 '%d' 需要類型 'int',但參數 2 的類型為 'int (*)(int *, int *, int)'

[英]warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int (*)(int *, int *, int)’

所以我是新來的,我的問題沒有得到很好的回應......所以我對其進行了編輯以提供更多信息。

我正在嘗試編寫一個程序,在主程序中調用兩個函數。 這是我到目前為止:

#include <stdio.h>

#define N 10

int inner_product(int a[], int b[], int n);
int inner_product_reverse(int a[], int b[], int n);

int main(void) {
int a[N], b[N], i;

printf("Enter the first array of size 10: ");
for (i = 0; i < N; i++)
    scanf("%d", &a[i]);

printf("Enter the second array of size 10: ");
for (i = 0; i < N; i++)
    scanf("%d", &b[i]);

printf("Inner product is: %d\n", inner_product);
printf("Inner product reverse is: %d\n", inner_product_reverse);
return 0;
}

int inner_product(int a[], int b[], int n) {
int sum = 0, i;
for (i = 0; i < N; i++)
    sum += (a[i] * b[i]);
return sum;
}

int inner_product_reverse(int a[], int b[], int n) {
int sum = 0, i;
for (i = 0; i < N; i++)
    sum += (a[i] * b[(N-1)-i]);
return sum;

但我收到標題中的錯誤。 我知道這是調用參數數量差異的問題,但我不確定如何以一種將函數的​​所有參數都考慮在內的方式編寫它。 我是否必須以某種方式將計算內積的 for 循環移動到主循環中? 謝謝。

問題在於警告已經說過的調用printf函數。

printf("Inner product is: %d\n", inner_product);
printf("Inner product reverse is: %d\n", inner_product_reverse);

inner_productinner_product_reverse是返回 int 並接受 3 個參數的函數: int (*)(int *, int *, int)

現在要獲得 int 結果,請調用這兩個函數。 您所做的是將它們的指針(位置)傳遞給 printf 函數,這是錯誤的。

您應該將代碼重寫為類似於以下內容:

//                               Parameters are added and function will be called
printf("Inner product is: %d\n", inner_product(a, b, i));
printf("Inner product reverse is: %d\n", inner_product_reverse(a, b, i));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM