目录
前言
一、问题描述
二、行参含义
三、题目应用
1、代码(重点在中文批注处)
2、执行测试
前言
在做CS50 Week3的problem set--plurality时,遇到main函数里带了两个行参(int argc, string argv[]),导致代码一下子没看懂。后来了解了它们的含义后才完成了这份作业。这篇文章通过CS50的plurality实例,解释了argc argv的作用。
一、问题描述
作业内容就是简单得找出获得投票数最高的人。
所有候选名单存放在一个数组里,但这些人的名字需要通过argc和argv输入。
二、行参含义
- arg:argument,参数。
- argc:argument count,参数数目。
- argv:argument vector,参数数组。
argv[0] 指向程序运行时的全路径名;
argv[1] 指向程序在DOS命令中执行程序名后的第一个字符串;
argv[2] 指向执行程序名后的第二个字符串;
……
argv[argc] 为NULL。
三、题目应用
1、代码(重点在中文批注处)
#include <cs50.h>
#include <stdio.h>
#include <string.h>// Max number of candidates
#define MAX 9// Candidates have name and vote count
typedef struct
{string name;int votes;
}
candidate;// Array of candidates
candidate candidates[MAX];// Number of candidates
int candidate_count;// Function prototypes
bool vote(string name);
void print_winner(void);int main(int argc, string argv[])
{// Check for invalid usageif (argc < 2){printf("Usage: plurality [candidate ...]\n");return 1;}// Populate array of candidatescandidate_count = argc - 1;if (candidate_count > MAX){printf("Maximum number of candidates is %i\n", MAX);return 2;}for (int i = 0; i < candidate_count; i++) //初始化candidates数组。{candidates[i].name = argv[i + 1]; //候选人名单要从argv[1]开始,因为argv[0]是可执行文件名,而从argv[1]开始的是通过命令行输入的第一个可变参数。candidates[i].votes = 0;}int voter_count = get_int("Number of voters: ");// Loop over all votersfor (int i = 0; i < voter_count; i++){string name = get_string("Vote: ");// Check for invalid voteif (!vote(name)){printf("Invalid vote.\n");}}// Display winner of electionprint_winner();
}// Update vote totals given a new vote
bool vote(string name)
{for (int i = 0; i < candidate_count; i++){if (strcmp(name, candidates[i].name) == 0){candidates[i].votes++;return true;}}return false;
}// Print the winner (or winners) of the election
void print_winner(void)
{int max = 0;for (int i = 0; i < candidate_count; i++){if (max < candidates[i].votes){max = candidates[i].votes;}}for (int i = 0; i < candidate_count; i++){if (max == candidates[i].votes){printf("%s\n", candidates[i].name);}}return;
}
2、执行测试
命令行中的“plurality”对应argv[0],“Zhang”对应argv[1],“Li”对应argv[2],“Sun”对应argv[3]。实际实现了通过命令行输入实参传递给main函数的方法,使所需要的候选人名单灵活性增加。
参考文章:
https://blog.csdn.net/Eastmount/article/details/20413773?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168492962916800211560794%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168492962916800211560794&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-4-20413773-null-null.142^v87^control,239^v2^insert_chatgpt&utm_term=main的argc&spm=1018.2226.3001.4187https://blog.csdn.net/Eastmount/article/details/20413773?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168492962916800211560794%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168492962916800211560794&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-4-20413773-null-null.142^v87^control,239^v2^insert_chatgpt&utm_term=main的argc&spm=1018.2226.3001.4187https://blog.csdn.net/qq_35635374/article/details/124483841?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168492962916800211560794%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168492962916800211560794&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-124483841-null-null.142^v87^control,239^v2^insert_chatgpt&utm_term=main的argc&spm=1018.2226.3001.4187系列文章目录提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加TODO:写完再整理文章目录系列文章目录前言一、main函数中的argc和argv两个形参的含义二、main函数中的argc和argv两个形参的调用原理1、以ROS一个简单功能包的main函数举例说明argc参数2、以ROS一个简单功能包的main函数举例说明argv参数总结前言认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长!本文先对进程中main函数中的argc和argv两个形参的含义及调https://blog.csdn.net/qq_35635374/article/details/124483841?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168492962916800211560794%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168492962916800211560794&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-124483841-null-null.142^v87^control,239^v2^insert_chatgpt&utm_term=main的argc&spm=1018.2226.3001.4187