声明函数指针返回数组 [英] Declaring a function pointer returning an array

查看:106
本文介绍了声明函数指针返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了练习,我正在尝试:

For practice, I'm trying to :

声明fubar为指向函数的指针,该函数采用指向char的指针并返回指向24个元素的数组的指针,其中每个元素均为指向struct foo的指针.

Declare fubar to be a pointer to a function that takes a pointer to a char and returns a pointer to an array of 24 elements where each element is a pointer to a struct foo.

我的逻辑是:

-fubar是指向带有char指针的函数的指针: (*fubar)(char*)

-fubar is a pointer to a function taking a char pointer: (*fubar)(char*)

-...返回一个指向24个元素的数组的指针,每个元素是一个结构foo:

-...returning a pointer to an array of 24 elems of where each elem is a struct foo:

(struct foo *)(*fubar)(char*)[24]

我的逻辑正确吗?

推荐答案

修复语法错误并删除struct foo *周围的括号后,

After fixing the syntax error and removing the parentheses around struct foo *,

struct foo* (*fubar)(char*)[24]

...您弄错的一部分是它实际上返回了一个 of 指针数组,而不是一个指向数组的指针.为了声明一个指向数组的指针,您需要额外的一组括号:

...the one part that you got wrong is that it actually returns an array of pointers, not a pointer to an array. In order to declare a pointer to the array, you need an extra set of parentheses:

struct foo (*(*fubar)(char*))[24]

您可以假装星星属于括号内的标识符(即数组的名称).

You can pretend that that the star belongs to an identifier (i.e., the name of the array) inside the parentheses.

这篇关于声明函数指针返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆