【发布时间】:2021-02-03 22:35:17
【问题描述】:
主要问题是:一旦我将字符串从一个函数发送到另一个函数,第二个函数并没有真正将字符串作为参数。
详细说明:
我有一个函数void myfunc() 包含一个单词。这个词应该被发送到另一个函数,所以它可以计算它的长度。这就是我到目前为止所写的:
void myfunc(int (*countlength)(char ch)){
char word[10] = "Hello\n";
int result = countlength(&word);
printf("Length of word: %d\n", result);
}
单词正在发送到这个函数countlength(char* word):
int countlength(char* word) {
int length = strlen(word);
return length;
}
但是函数countlength() 无法计算它的长度,我不知道为什么......
问题是,当单词在 main 函数中时它会起作用。有人知道为什么我的代码不起作用吗?
【问题讨论】:
-
将
countlength(&word)更改为countlength(&word[0])或countlength(word) -
int (*countlength)(char)是一个指向接受char的函数的指针。你的意思是写int (*countlength)(char*)? -
为什么函数 myfunc 有
int (*countlength)(char ch)作为参数?
标签: c function-pointers c-strings strlen function-declaration