gpt4 book ai didi

c++ - 代码块未正确显示程序参数

转载 作者:行者123 更新时间:2023-11-30 21:35:20 25 4
gpt4 key购买 nike

我的代码在c中如下:

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
int i;
for (i=0;i<argc;i++)
{
printf("Hello world! The arguments are %d, argc is %d and the string is %s\n",argc,i,argv);
}

return 0;
}

我无法在输出中正确看到参数。它有点加密。

我去了项目->设置程序参数。但它不起作用。请帮忙?

最佳答案

您正在打印一个指向指针的指针,正如编译器告诉您的那样:

test.c:12:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char **’ [-Wformat=]
printf("Hello world! The arguments are %d, argc is %d and the string is %s\n",argc,i,argv);
^

argv 是一个指针数组,您想要打印该数组中每个项目所指向的字符串:

更正的代码:

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
int i;
for (i=0;i<argc;i++)
{
printf("Hello world! The arguments are %d, argc is %d and the string is %s\n",argc,i,argv[i]);
}

return 0;
}

关于c++ - 代码块未正确显示程序参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37407321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com