- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在努力理解这段代码的某些部分。它要求输入一些字符串,计算元音并显示结果。这是一些我不理解的定义,但我理解的机制。
在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
分配一些空间,并将字符串复制到那里。 entrada
在 cad
中设置相应的指针(将其视为 cd
)以指向分配的缓冲区。
当您传递数组作为参数时,您并不是在传递副本。相反,第一个元素的地址被传递给函数。这就是 entrada
修改 cad
中指针的方式。
关于C语言 : args in functions inside Main (). 不清楚它们是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10556481/
MVC3 中的助手是否在 Controller 和 View 中使用? 助手是放置常用 Controller 方法的正确位置吗? 我想创建一个通用方法来获取数据库中的所有子子 ID,并确保它位于正确的
有人可以阐明这两者之间的区别吗,因为它们存在于执行上下文中?我很难阅读 ECMA 262 v 5规范并清楚地看到差异。 谢谢你, 最佳答案 两者都是执行上下文的组件(相同类型),但它们有不同的用途(f
我是一名优秀的程序员,十分优秀!