如何在C中找到argv[]的长度 [英] How to find the length of argv[] in C

查看:31
本文介绍了如何在C中找到argv[]的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

int main(int argc, char *argv[]){
    int fir; //badly named loop variable
    char *input[] = calloc( strlen(argv), sizeof(char)); //initializing an array
    for( fir = 1; fir< strlen(argv); fir++){ //removing the first element of argv
        strcat(input, argv[fir]); // appending to input
    }
}

我得到的错误是第 7 行.它说从不兼容的指针类型传递 'strlen' 的参数 1".对于 strcat 函数,我遇到了同样的错误.它还说给定一个 char ** 但预期两个函数都有一个 const char *".

The error I'm getting is for line 7. It says "passing argument 1 of 'strlen' from incompatible pointer type". I get the same error for the strcat function. It also says "given a char ** but expected a const char *" for both functions.

我试图用 argv 的所有元素填充一个新数组,除了第一个.我试过 argv = &argv[1] 但它没有用.

I'm trying to populate a new array with all the elements of argv except the first. I tried argv = &argv[1] and it did not work.

strlen()strcat() 函数不接受 char 数组吗?

Do the strlen() and strcat() functions not take char arrays?

推荐答案

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

argv 是一个指向 char 的指针数组(即字符串数组).这个数组的长度存储在 argc 参数中.

argv is an array of pointers to char (i.e. array of strings). The length of this array is stored in argc argument.

strlen 用于检索必须以空字符结尾的单个字符串的长度,否则行为未定义.

strlen is meant to be used to retrieve the length of the single string that must be null-terminated else the behavior is undefined.

这篇关于如何在C中找到argv[]的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆