C语言:Main()内函数中的参数。不清楚是什么

C语言:Main()内函数中的参数。不清楚是什么,c,main,args,C,Main,Args,我一直在努力理解这段代码的某些部分。它要求输入一些字符串,计算元音并显示结果。这是一些我不理解的定义,我不理解的机制 在main()内的定义中。我不明白在entrada函数中这个(cad)是什么参数。上面的一行定义了一个指向char的3个指针的数组,如果我没有记错的话,也就是char*cad[N]。我想说,我的问题是主函数中的所有内容,以及函数括号内的参数的意义。之后我就明白了 # include<stdio.h> # include<stdlib.h> # includ

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

在main()内的定义中。我不明白在entrada函数中这个(cad)是什么参数。上面的一行定义了一个指向char的3个指针的数组,如果我没有记错的话,也就是char*cad[N]。我想说,我的问题是主函数中的所有内容,以及函数括号内的参数的意义。之后我就明白了

# 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]);
    }
}
#包括
#包括
#包括
#包括
#定义n3
//功能原型
void salida(char*[],int*);
无效截留(字符*[]);
内部人声(字符*);
int main()
{
char*cad[N];//声明指向char的3个指针的数组
int j,voc[N];//声明int和int数组
entrada(cad);//用于读取字符串的函数。
//数一数每行有多少个元音

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


将数组作为参数传递时,不是传递副本。而是将第一个元素的地址传递给函数。如果使用调试器(例如,
gdb
),这就是
entrada
可以修改
cad

中指针的方式你可以问它的名字是什么类型,例如“代码> Pad CAD < /COD>或<代码> pSaleSaldI/Cuth>可以考虑使用Linux,<代码> GDB 很好地工作在它上,但是你应该编译你的源有警告和调试信息,例如:代码> Gcc-WALL-G/<代码>谢谢,我自己在代码中写了注释,并且清晰地显示了密码。cad[N]是一个指针数组,但是,普通的“cad”,也是作为函数entrada的参数出现在括号中的。我理解函数entrada,正是这种混淆使我把char*cad[N]看作是一个指针数组,而cad就是它。@iaintunderstand:
char*cad[N]
什么都不是。它声明
cad
是一个指针数组。谢谢你,很抱歉我在语法和声明方面遇到了问题。我会记住这个教学。
char*cad[N];
是一个声明。声明的意思就是“名称
cad
指的是一个数组
char*
。基本上,您创建了一个数组,并将其命名为
cad
。从那时起,您可以使用名称来指代数组。变量也一样:
int i;
表示“留出一个空间来容纳
int
并将其命名为
i
”.现在还有一点更棘手。名称
cad
在声明它的范围内可见(因此,在函数
main
)。如果您在另一个函数中声明了也称为
cad
的内容,它将引用完全不同的内容。当您将
cad
传递给
entrada
时,
cd
将指向与
cad
相同的内存位置。