c - 我的函数中省略了参数名称

标签 c

函数“histogram”中省略了参数名称。我必须对“直方图”功能进行哪些更改?该程序正在读取单词并打印它们的长度(直方图)。 这是 main() 函数:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 99
#define M 99

int main(int argc, char *argv[])
{
  int a, j, count = 0, i, b = 0, word = 0, sum = 0;
  char x[N][M];

  for(i=0; i<N; i++) {
    printf("eisagete leksi,\ngrapste ****telos gia exit: ");
    scanf("%s", x[i]);
    a = strcmp(x[i], "****telos");
    if(a==0) break;
    count++;
  }

  for(i=0;i<count;i++)
    printf("%d leksi: %s\n",i+1, x[i]);

  printf("\n");

  for (j=0;j<count;j++){
    printf("%d :" , j+1);
    for(i=0; i<strlen(x[j]) ;i++){
    printf("*");}
    printf("\n\n");
  }

  while ((a=epilogi())!=5){
    switch (a){
      case 1: metrisileksewn(x);break;
      case 2: metrisixaraktirwn(x);break;
      case 3: diaflekseis(x);break;
      case 4: istograma(x);break;
      default:break;
    }
  }
}

我的功能之一是:

void histogram(char[N][M]){
  int i, j, count;
  char x[N][M];
  for (j=0; j<count; j++){
    for(i=0; i<strlen(x[j]); i++)
      printf("*");
    printf("\n");
  }
}

最佳答案

您需要提供参数的名称。

void histogram(char y[N][M])

您可以省略函数原型(prototype)声明中的名称,但不能省略定义。

您还需要考虑是否仍然需要 x,或者是否应将参数命名为 x 并删除局部变量 - 我认为这可能是您所需要的。但您还必须担心 count — 它在哪里初始化?它可能应该是函数的额外参数。您应该使用 putchar() 来放置单个字符;当没有包含至少一个转换规范的格式字符串时,最好不要使用 printf()

void histogram(char x[][M], int count)
{
    for (int j = 0; j < count; j++)
    {
        int len = strlen(x[j]);
        for (int i = 0; i < len; i++)
            putchar('*');
        putchar('\n');
    }
}

关于c - 我的函数中省略了参数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23327928/

相关文章:

c - gtk+ 在调用任何 cairo 绘图函数时崩溃

c - fgets 函数不保存字符串中的第一个字母

c - 数组在 C 中给出不同的输出?

c - 在 C 中类型转换变量的性能开销

c - 如何仅扫描范围内的数据,而不是保存整个数据?

c - Sockets_Client_Server_communication_Linux_C

c - 从 C 中的 stdin 读取数据

c - 对于字符串,查找并替换

计数器变量问题

c - C语言中如何比较两个数组?