c - 数组到链表

标签 c

这是 C 语言。

我必须从命令行获取信息(例如../command # # #),然后通过命令运行该信息,然后打印后续信息。

int *numbers = malloc(sizeof(argc)*sizeof(int));
int i = 0;
while (i<argc) {
    numbers[i] = atoi(argv[i+1]);
    i++;
   }

Node *a = arrayToList(numbers,sizeof(numbers)/sizeof(int));
Node *b = mapList(decrement,a);
displayList(b);

return 0;

它的目的是返回减一的值,但我正在努力将数字读入原始数组。

有什么想法吗?

最佳答案

代码的第一部分应该是

if ( argc < 2 )
    exit( 1 );     // need at least one arg after the command name

int numbers[argc-1];
for ( int i = 1; i < argc; i++ )
    numbers[i-1] = atoi(argv[i]);

您需要为int分配空间,然后处理从1argc-1的args。

关于c - 数组到链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29379476/

相关文章:

c - 如何在现有数组元素中追加数据而不覆盖元素中的数据

c - 如何返回一个结构?

c - 在 C 中打印 trie 中的所有单词

c - 如何在不使用通用指针的情况下引用指向数据或数据的指针?

c - 如何计算子弹击中的位置

c - Linux 中的 Pthread

c - 使用 .d-dependency-file 时 makefile 先决条件重建失败

c++ - 用 C 和 C++ 接口(interface)编写库,用哪种方式包装?

c - 在 Windows 中,如何阻止 fprintf 将\r 与\n 一起打印到文件

c - readline.h 不在 32 位 ubuntu 中,而是在 64 位中?