将数组字符串传递给函数指针?

时间:2017-05-30 20:17:55

标签: c

我想某些事情根本就是错误的,我想发送这个全球性的:

static char content[MAX_NUM_WORDS][MAX_WORD_LEN];

作为函数指针的参数,其中函数指针def:

void(*flashReadDelegate)(char*[])=0;

并用:

调用它
//save some data in (which prints ok)
strcpy(content[record_desc.record_id],toSave);

// ***Send the delegate out
(*flashReadDelegate)(content);  // ** here there is a compiler warnning about the argument

那么,如果我想发送content

,指针参数应该如何?

2 个答案:

答案 0 :(得分:5)

void(*flashReadDelegate)(char*[])=0;错了。你的函数指针应该是这样的

void (*flashReadDelegate)(char (*)[MAX_WORD_LEN]);  

您尚未提及flashReadDelegate所指向的函数的原型。我假设它的原型是

void func(char (*)[MAX_WORD_LEN]);

现在,在函数调用(*flashReadDelegate)(content);中,参数数组content将转换为指向MAX_WORD_LEN char s((*)[MAX_WORD_LEN]数组的指针)。

答案 1 :(得分:1)

您对content的声明不是指向字符串的指针。它是一个 MAX_NUM_WORDS个MAX_WORD_LEN个字符串的数组。

如果你想要一个字符串数组,你需要将content声明为:static char * content [MAX_NUM_WORDS];`