简体   繁体   English

我正在尝试使数组在 c 中打印出来

[英]I'm trying to make an array print itself in c

#include <stdio.h>

int table [100];

int main (void)
{
    for (int i=0; i<100; i++)
    {
        i = table [i];
        printf("%i\n", table[i]);
    }
}

I'm trying to make an array that stores 0 to table[0], 1 to table [1] and so forth and then prints it's own value.我正在尝试制作一个将 0 存储到 table[0]、1 到 table [1] 等等的数组,然后打印它自己的值。

Right now the code outputs 0's.现在代码输出0。

您的分配倒退了 - 您需要将i分配给table[i] ,而不是像您目前拥有的相反:

table[i] = i;
#include <stdio.h>

int table [100];

int main (void)
{
    for (int i=0; i<100; i++)
    {
        table [i] = i;
        printf("%i\n", i);
    }
}

There you go.你去吧。 With nothing in the array and the iterator being reset every loop, it won't even print all hundred zeroes, just 0#0 and 0#1 forever.由于数组中没有任何内容并且迭代器在每次循环中都被重置,它甚至不会打印所有一百个零,永远只打印 0#0 和 0#1。 Now.现在。 Alternatively...或者...

#include <stdio.h>

int table [] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
    21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
    41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
    61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
    81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99};

int main (void)
{
    for (int i=0; i<100; i++)
    {
        printf("%i\n", table[i]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM