简体   繁体   English

无法将传递数组理解为命令行参数

[英]Unable to understand passing array as Command-line-argument

/* /*

I am trying to program a Tideman alternative method algorithm.我正在尝试编写一个 Tideman 替代方法算法。
I am taking i/p through command-line-argument.我正在通过命令行参数获取 i/p。
When I try to print the populated candidate array.当我尝试打印填充的候选数组时。
It is printing single characters when format specifier is char type.当格式说明符为 char 类型时,它正在打印单个字符。 The result of other specifier is attached.附加其他说明符的结果。
当格式说明符为字符串时 当格式说明符为整数时 当格式说明符为字符时 */ */

/* /*

Same logic when executed on CS50 IDE, it prints the strings as expected.在 CS50 IDE 上执行相同的逻辑,它按预期打印字符串。
I mean if the CLA is Alice Bob Charlie, then at 0th index Alice and so on...我的意思是如果 CLA 是 Alice Bob Charlie,那么在第 0 个索引 Alice 等等......
This step is crucial as later the names from the voters would be compared.这一步至关重要,因为稍后将比较选民的名字。
I don't how to proceed from here.我不知道如何从这里开始。 I came to know only about this when I reached the comparison stage.当我到达比较阶段时,我才知道这一点。 */ */

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

#define MAX 9

int preferences[MAX][MAX]; 


bool locked[MAX][MAX];

typedef struct
{
    int winner;
    int loser;
}
pair;

char candidates [MAX] = {0};
pair pairs[MAX*(MAX-1)/2];


int pair_count = MAX;
int candidate_count = MAX;

//Prototypes
bool vote (int rank, char name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs();
void sort_pairs();
void lock_pairs();
void print_winner ();

int main(int argc, char *argv[])
{

//Check for invalid usage.  
    if (argc < 4)
    {
        printf("Usage: tideman [candidate ...]\n");
        return 1;
    }

    candidate_count = argc - 1;
    if (candidate_count > MAX)
    {
        printf("Maximum number of candidates is %i\n", MAX);
        return 2;
    }
//popuplate the candidate array.  
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i] = argv[i+1];
        printf("Candidate array: %c\n", *argv[i+1]);
    }
}

Your problem is understanding how arguments work.您的问题是了解 arguments 的工作原理。 Consider this line in the terminal: ./main tideman abc How does argv looks like?考虑一下终端中的这一行: ./main tideman abc It is an array.. with 3 elements in it:它是一个数组.. 里面有 3 个元素:

argv[0] => "main"
argv[1] => "tideman"
argv[2] => "abc"

Lets say instead of abc you have ./main tideman ab c .可以说,而不是abc你有./main tideman ab c How does argv looks like? argv 长什么样子? It is an array.. with 5 elements in it:它是一个数组.. 里面有 5 个元素:

argv[0] => "main"
argv[1] => "tideman"
argv[2] => "a"
argv[3] => "b"
argv[4] => "c"

candidates is a char array, so it can hold characters. candidates是一个字符数组,所以它可以容纳字符。 Now lets consider first example ( ./main tideman abc ).现在让我们考虑第一个示例( ./main tideman abc )。 What is the for-loop in your code does?你的代码中的for循环是做什么的? candidates[0] = argv[1] . candidates[0] = argv[1] What is wrong here?这里有什么问题? candidates[0] is the first character in the candidates array.. Cool. candidates[0]是候选数组中的第一个字符。很酷。 argv[1] is "tideman" .. a string. argv[1]"tideman" .. 一个字符串。 I hope you can see the assignment problem here.我希望你能在这里看到分配问题。

Now the second problem, in the second part of the loop.现在是第二个问题,在循环的第二部分。 With the first iteration, argv[0+1] is "tideman" Now you want to print the string tideman to the screen.在第一次迭代中, argv[0+1]"tideman"现在您想将字符串tideman打印到屏幕上。 The command print("%s\n") does print a string, and it take as argument pointer to that string.命令print("%s\n")确实打印了一个字符串,并将其作为指向该字符串的参数指针。 What is the pointer?指针是什么? its argv[1] .. which is a pointer to the first element in the argv[1] array.它的argv[1] .. 它是指向argv[1]数组中第一个元素的指针。 What is *argv[1] ?什么是*argv[1] Thats right, its the value of the first character in the argv[1] array.. So in our example argv[1] is "tideman", so *argv[1] is 116 which is the ascii value for t .没错,它是argv[1]数组中第一个字符的值。所以在我们的示例中argv[1]是“tideman”,所以*argv[1]116 ,这是t的 ascii 值。

So in order to achieve your goal of printing "tideman" , which is argv[1] you just need printf("%s\n", argv[1]);因此,为了实现打印"tideman"的目标,即argv[1]您只需要printf("%s\n", argv[1]); with argument argv[1] you supply pointer to the first character of the string, which is what %s ask for.使用参数argv[1]提供指向字符串第一个字符的指针,这是%s所要求的。

So to recap you have 2 problems in your for-loop, and you could see both of those problem as warnings in the terminal.因此,回顾一下您的 for 循环中有 2 个问题,您可以将这两个问题都视为终端中的警告。

In C language, a char is a single character.在 C 语言中, char是单个字符。 A string is by convention a null terminated character array.按照惯例,字符串是 null 终止的字符数组。 For that reason, it is common to use char * to represent strings.因此,通常使用char *来表示字符串。 And it is up to the programmer to make sure that the life time of the pointed array does not end before the value is used.并且由程序员来确保指向数组的生命周期在值被使用之前不会结束。

Warnings are to be observed.必须遵守警告。 Unless you know what a warning means and why you want to ignore it, you really should treat them as errors.除非您知道警告的含义以及为什么要忽略它,否则您确实应该将它们视为错误。

Here the information was in the first warning you have shown:这里的信息在您显示的第一个警告中:

    candidates[i] = argv[i + 1];

... warning: assignment to char from char *...

It means that argv[i+1] does represent a string, but you try to assign it into a single char which invokes Undefined Behaviour.这意味着argv[i+1]确实代表了一个字符串,但是您尝试将它分配给一个调用未定义行为的单个char

Here is a quick fix for your current code:这是您当前代码的快速修复:

...
char *candidates [MAX] = {NULL};  // candidates is an array of strings
...
    //popuplate the candidate array.  
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i] = argv[i + 1];
        printf("Candidate array: %s\n", argv[i + 1]);    // use %s in C to display a string
    }
...

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

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