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

标签 c++ c codeblocks

我的代码在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/

相关文章:

c++ - 测试返回三角形类型的函数

C++ 函数未按预期工作

c - 从 Go 中读取 C 类型的字符串

c - 使用位域作为 c 中整数的表示

c++ - 扩展数组时出错 : no operator found which takes

c++ - quickfix/c++ 中的重复组

c - 为什么我们在使用单指针传递给函数时必须对二维数组进行类型转换?

c++ - 从 2020 年开始学习 c++ 需要 c++17 吗?

代码块 : Undefined reference to myFct

c++ - 在 dos.h 中为 c/c++ 函数声音/延迟/nosound