【问题标题】:passing argument 1 of ‘function_name’ makes pointer from integer without a cast传递“function_name”的参数 1 使指针从整数而不进行强制转换
【发布时间】:2016-11-25 18:57:06
【问题描述】:

下面是代码:

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

void draw(int length, char brush);
int stringLength(char name[]);
void drawName(char nameLetter[], char brush);

int length;
char brush, name;
char *a = "Anne", *b = "Bart", *c = "Celine", *d = "Darius";

int main(void) {
  printf("Write down the length of line.\n");
  scanf("%d", &length);
  printf("Give first letter of the name (a, b, c albo d)\n");
  scanf(" %c", &name);
  printf("Pick character to be used as brush.\n");
  scanf(" %c", &brush);
  drawName(name, brush);
  return 0;
}

void draw(int length, char brush) {
  int i;
  for (i = 0; i < length; i++) {
    printf("%c", brush);
  }
}

int stringLength(char name[]) {
  int i;
  while (name[i] != '\0') {
    i++;
  }
  return i;
}

void drawName(char nameLetter[], char brush) {
  draw(stringLength(nameLetter), brush);
  printf("%s\n", nameLetter);
  draw(stringLength(nameLetter), brush);
}

我在这里尝试做的是获取字符串的长度并将该值用作另一个函数中的参数,该函数负责连续打印尽可能多的字符,因为给定字符串中有字符。但是我在第 19 行收到一个错误:(传递 'drawName' 的参数 1 使指针从整数而不进行强制转换).. 我已经在 stackoverflow 上阅读了与此问题类似的所有问题,但仍然无法使其工作.非常感谢您的帮助!我这里是死胡同……

【问题讨论】:

  • drawNamechar * 作为其参数(从char [] 衰减)。您的代码显然传递了一个 char 参数。这有什么不明白的?
  • stringLength() 中你未能初始化i

标签: c function parameters declaration


【解决方案1】:

功能

void drawName(char nameLetter[], char brush);

char *类型的第一个参数声明

当您使用char 类型的参数调用它时。

char brush, name;
^^^^^^^^^^^^^^^^
//...            
drawName(name, brush);

【讨论】:

  • 每当我将 char 名称更改为 char * 名称时,我都会收到错误:(格式 '%c' 需要 'char *' 类型的参数,但参数 2 的类型为 'char **')跨度>
  • @angrysomoan 如果要输入字符串,则必须将 name 声明为字符数组并至少使用格式说明符 %s,或者您可以使用标准函数 fgets 而不是 scanf。
  • 我正在尝试获取一个字符,并使用它来指向字符串名称(例如:获取 char a,并将其用作函数中的参数以指向名为 a 的字符串变量)。该怎么做?
  • @angrysomoan 这是不可能的。
  • 我正在尝试获取一个字符,并使用它来指向字符串名称(例如:获取 char a,并将其用作函数中的参数以指向名为 a 的字符串变量)。 这太奇怪了。你可能有一个XY Problem
猜你喜欢
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 2021-08-13
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多