我想获得一个很长的数字,计算它有多少数字,然后取该数字的第二个数字并乘以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'是什么意思吗? –