C语言 : args in functions inside Main (). 不清楚它们是什么

标签 c program-entry-point args

我一直在努力理解这段代码的某些部分。它要求输入一些字符串,计算元音并显示结果。这是一些我不理解的定义,但我理解的机制。

在main()内部的定义中。我不明白这个“(cad)”在 entrada 函数中的参数是什么。它上面的一行定义了一个由 3 个指向 char 的指针组成的数组,如果我正确地相信的话,即 char *cad[N] 。我想说我的问题是 Main 函数中的所有内容,函数括号内的参数如何有意义。后来我就明白了。

# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# include<ctype.h>
# define N 3


// Function Prototypes

void salida(char *[], int*);
void entrada(char *[]);
int vocales(char *);

int main ()
{
    char *cad[N];  // declaring an array of 3 pointers to char
    int j, voc[N]; // declaring ints and an array of ints
    entrada (cad);// Function to read in strings of characters. 
    // count how many vowels per line
    for (j = 0; j<N; j++)
    voc[j] = vocales(cad[j]); // it gets the string and sends it to function vocales to count how many vowels. Returns number to array voc[j]
    salida (cad, voc);
}

// Function to read N characters of a string
void entrada(char *cd[] ){
    char B[121]; // it just creates an array long enough to hold a line of text
    int j, tam;

    printf("Enter %d strings of text\n", N );

    for (j= 0; j < N; j++){
        printf ("Cadena[%d]:", j + 1);
        gets(B);
        tam = (strlen(B)+1)* sizeof(char); // it counts the number of characters in one line
        cd[j] = (char *)malloc (tam); // it allocates dynamically for every line and array index enough space to accommodate that line
        strcpy(cd[j], B); // copies the line entered into the array having above previously reserved enough space for that array index
    } // so here it has created 3 inputs for each array index and has filled them with the string. Next will be to get the vowels out of it

}

// Now counting the number of vowels in a line
int vocales(char *c){
    int k, j;

    for(j= k= 0; j<strlen(c); j++)
        switch (tolower (*(c+j)))
        {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
              k++;
              break;
        }
    return k;
}

// function to print the number of vowels that each line has
void salida(char *cd[], int *v)
{
    int j;

    puts ("\n\t Displaying strings together with the number of characters");
    for (j = 0; j < N; j++)
    {
        printf("Cadena[%d]: %s has %d vowels \n", j+1, cd[j], v[j]);
    }
}

最佳答案

cad 是一个指针数组。它只有 N 个指针的空间,而不是实际的字符串数据。 entrada 函数读取 N 个文本字符串。对于其中每一个,它都会使用 malloc 分配一些空间,并将字符串复制到那里。 entradacad 中设置相应的指针(将其视为 cd)以指向分配的缓冲区。

当您传递数组作为参数时,您并不是在传递副本。相反,第一个元素的地址被传递给函数。这就是 entrada 修改 cad 中指针的方式。

关于C语言 : args in functions inside Main (). 不清楚它们是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10556481/

相关文章:

c - 仅在第一次调用函数时在函数内部进行 fork()

arrays - 在C中交换2个数组

c - 架构无关代码

c++ - 为 main() 返回不同的 int 值

c - 为什么我的替换某个字符的函数只对该字符的第一个实例执行此操作?

Windows 上的 Python 2.7,所有多处理示例的 "assert main_name not in sys.modules, main_name"

python - 将任意数量的不同列表或元组转换为一个列表的更 Pythonic 方式

python - 函数无法识别 args 和 kwargs

Docker 无法在 docker-compose 中使用参数(使用 .env 中的参数)

java - 遍历所有 main() 参数