C 格式‘;%d’;应为类型为‘;int’;,但参数3的类型为‘;int*’;

C 格式‘;%d’;应为类型为‘;int’;,但参数3的类型为‘;int*’;,c,C,我正在尝试创建一个程序来显示int数组中的元素。但我一直收到警告。这可能会被否决,但我不知道我在哪里犯了错误 代码如下: #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <math.h> #include <time.h> int main() { int counter; in

我正在尝试创建一个程序来显示int数组中的元素。但我一直收到警告。这可能会被否决,但我不知道我在哪里犯了错误

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>

int main() {
     int counter;
     int elements[3] = { 22, 52, 95 };

     for (counter = 1; counter <= 3; counter++) {
         printf("Element %d: %d\n", counter, elements);
     }
     return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int main(){
整数计数器;
int元素[3]={22,52,95};

对于(counter=1;counter这里的问题是,您试图打印
int
,但
元素
不是
int

它是一个数组

  • 您希望使用
    []
    一次检索一个
    int
  • C
    中的数组是
    0
    索引的。因此,这意味着您应该从
    0
    th元素开始访问它们
检查以下各项:

for(counter = 0; counter < 3; counter++)
     printf("Element %d: %d\n", counter, elements[counter]);
for(计数器=0;计数器<3;计数器++)
printf(“元素%d:%d\n”,计数器,元素[计数器];

这里的问题是,您试图打印
int
,但
元素
不是
int

它是一个数组

  • 您希望使用
    []
    一次检索一个
    int
  • C
    中的数组是
    0
    索引的。因此,这意味着您应该从
    0
    th元素开始访问它们
检查以下各项:

for(counter = 0; counter < 3; counter++)
     printf("Element %d: %d\n", counter, elements[counter]);
for(计数器=0;计数器<3;计数器++)
printf(“元素%d:%d\n”,计数器,元素[计数器];
我想你是说

for(counter = 0; counter < 3; counter++)
{
    printf("Element %d: %d\n", counter, elements[counter]);
}
for(计数器=0;计数器<3;计数器++)
{
printf(“元素%d:%d\n”,计数器,元素[计数器];
}
编辑: 说明:循环应该从0开始运行,因为“C”数组是基于0的,如果最大值为“n-1”,其中n是数组大小。下一个%d将需要元素而不是数组本身。要访问数组元素,您需要使用“[]”运算符。

我想您的意思是

for(counter = 0; counter < 3; counter++)
{
    printf("Element %d: %d\n", counter, elements[counter]);
}
for(计数器=0;计数器<3;计数器++)
{
printf(“元素%d:%d\n”,计数器,元素[计数器];
}
编辑:
说明:循环应该从0开始运行,因为“C”数组是基于0的,如果最大值为“n-1”,其中n是数组大小。下一个%d将需要元素而不是数组本身。要访问数组元素,您需要使用“[]”运算符。

这是您更正的代码,其中包含有关每个更改的注释

#include <stdio.h>    // printf()
// only include headers those contents are actually used
//#include <stdlib.h>
//#include <ctype.h>
//#include <string.h>
//#include <math.h>
//#include <time.h>

int main( void ) // only two valid signatures for 'main()'
                 // 'int main( void )' and
                 // 'int main( int argc, char *argv[] )'
{
     size_t counter;  // sizeof() returns a 'size_t' not an 'int'
     int elements[] = { 22, 52, 95 }; // compiler can make the count

     // indexes start at 0 and end at number of elements -1
     // don't hard code 'magic' numbers.  In this case let the compiler do the work
     for (counter = 0; counter < sizeof(elements)/sizeof(int); counter++)
     {
         // remember to index the array
         // remember that an array name, by itself,
         // degrades to the first address of the array
         // the 'counter' is now a 'size_t' so the format specifier must match
         printf("Element %lu: %d\n", counter, elements[ counter ]);
     }

     return 0;
}
#包括//printf()
//仅包含那些实际使用的内容的标题
//#包括
//#包括
//#包括
//#包括
//#包括
int main(void)//对于“main()”只有两个有效签名
//“int main(无效)”和
//'int main(int argc,char*argv[])'
{
size\u t counter;//sizeof()返回一个'size\t'而不是'int'
int元素[]={22,52,95};//编译器可以进行计数
//索引从0开始,在元素数-1结束
//不要硬编码“神奇”数字。在这种情况下,让编译器来完成这项工作
对于(计数器=0;计数器
这是您更正的代码,其中包含了有关每次更改的注释

#include <stdio.h>    // printf()
// only include headers those contents are actually used
//#include <stdlib.h>
//#include <ctype.h>
//#include <string.h>
//#include <math.h>
//#include <time.h>

int main( void ) // only two valid signatures for 'main()'
                 // 'int main( void )' and
                 // 'int main( int argc, char *argv[] )'
{
     size_t counter;  // sizeof() returns a 'size_t' not an 'int'
     int elements[] = { 22, 52, 95 }; // compiler can make the count

     // indexes start at 0 and end at number of elements -1
     // don't hard code 'magic' numbers.  In this case let the compiler do the work
     for (counter = 0; counter < sizeof(elements)/sizeof(int); counter++)
     {
         // remember to index the array
         // remember that an array name, by itself,
         // degrades to the first address of the array
         // the 'counter' is now a 'size_t' so the format specifier must match
         printf("Element %lu: %d\n", counter, elements[ counter ]);
     }

     return 0;
}
#包括//printf()
//仅包含那些实际使用的内容的标题
//#包括
//#包括
//#包括
//#包括
//#包括
int main(void)//对于“main()”只有两个有效签名
//“int main(无效)”和
//'int main(int argc,char*argv[])'
{
size\u t counter;//sizeof()返回一个'size\t'而不是'int'
int元素[]={22,52,95};//编译器可以进行计数
//索引从0开始,在元素数-1结束
//不要硬编码“神奇”数字。在这种情况下,让编译器来完成这项工作
对于(计数器=0;计数器
数组从0开始,而您忘记在循环
printf(“元素%d:%d\n,计数器,元素”)中使用数组访问语法;
-->
printf(“元素%d:%d\n,计数器,元素[counter-1]);
数组从0开始,而您忘记在循环
printf(“元素%d:%d\n,计数器,元素”)中使用数组访问语法;
-->
printf(“元素%d:%d\n”,计数器,元素[counter-1]);