我想獲得一個很長的數字,計算它有多少數字,然後取該數字的第二個數字並乘以2,然後遍歷數字的其餘部分2nd,然後將其添加到數組。如果你們知道我在說什麼,這是從cs50設置的信用卡問題。C - 返回數組不工作
當我把它拋出回這個錯誤的程序:
error: format specifies type 'long' but the argument has type '<dependent type>' [-Werror,-Wformat]
credit.c:22:21: error: use of undeclared identifier 'array'
printf("%ld\n", array);
^
credit.c:40:8: error: incompatible pointer to integer conversion returning 'long [nDigits]' from a function with result type 'long' [-Werror,-Wint-conversion]
return array;
^~~~~
3 errors generated.
make: *** [credit] Error 1
代碼:
#include <cs50.h>
#include <stdio.h>
#include <math.h>
long find_2ndlast_nums(long x);
int main(void)
{
long n;
long nDigits;
do
{
n = get_long_long("Please enter your credit card number:\n");
nDigits = floor(log10(labs(n))) + 1;
}
while (nDigits < 13 || nDigits > 16);
find_2ndlast_nums(n);
printf("%ld\n", array);
}
long find_2ndlast_nums(long x)
{
long nDigits = floor(log10(labs(x))) + 1;
long array[nDigits];
for (int y = 1; y < nDigits; y += 2)
{
x = (fmod((floor(x/10^y)), 10)) * 2;
array[nDigits - y] = x;
}
return array;
}
這已被問了很多次,你不能從函數返回一個數組。您需要使用'malloc()'或將該數組作爲參數傳遞給函數,如果需要,可以在函數中對其進行修改。 –
您的函數被聲明爲返回'long',並且您嘗試返回'long [nDigits]'。拋開內存問題,你爲什麼認爲這會起作用? – ApproachingDarknessFish
在'long find_2ndlast_nums(long x)'中,你明白第一個'long'是什麼意思嗎? –