为什么我的整数数组指针不起作用?

时间:2013-04-01 17:31:02

标签: c arrays pointers

问题在于:http://cps125.scs.ryerson.ca/labs/homework8.gif

这是我的尝试:

#include <stdio.h>

void tenfold(int (*array1)[9], int (*array2)[9])
{
    int i;
    for (i = 0; i < 9; i++) {
        if ((*array1)[i] > 0) {
            (*array2)[i] = 10 * (*array1)[i];
        } else {
            (*array2)[i] = (*array1)[i];
        }
    }
}

int main()
{
    int array1[9] = { 3, 4, 5, 6, 7, -8, -9, 1, 2 }, array2[9], i;

    tenfold(&array1, &array2);

    for (i=0; i < 9; i++) {
        printf("%lf ", array2[i]);
    }
    return 0;
}

这是输出:

44100667367791664000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000.000000 441006673677917200000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000.000000 4410066736779177 60000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000.000000 4410066736779183200000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000.000000 44100667367791888000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000.000000 44100691391845304000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000.000000 441006913918452980000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000.000000 4410066736779155 20000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000.000000 4410066736779160800000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000.000000

3 个答案:

答案 0 :(得分:4)

这里至少存在一个问题:

printf("%lf ", array2[i]);

%lf字面意思是“长浮动”,但是你想打印整数:

printf("%i\n", array2[i]);

还要记住printf等待\n刷新。

编辑:正如newacct指出的那样,%f%lf最后都是double,因为varargs将float提升为double

答案 1 :(得分:0)

无需在函数tenfold

的输入数组定义中添加星号

将其定义为以下

void tenfold(int array1[9], int array2[9])

使用*array1进入函数array2

时删除星号tenfold
void tenfold(int array1[9], int array2[9])
{
    int i;
    for (i = 0; i < 9; i++) {
        if ((array1)[i] > 0) {
            (array2)[i] = 10 * (array1)[i];
        } else {
            (array2)[i] = (array1)[i];
        }
    }
}

当您在tenfold中调用main函数时:

tenfold(array1, array2);

答案 2 :(得分:0)

“%lf”是float的格式,而不是int。