complex pointer declaration

J

junky_fellow

Guys,

I am sorry for asking so many questions.

I need to declare a pointer to an array of pointer to functions
where each function takes void as an argument and returns int.

I could declare an array of pointers to functions, but not the
complete thing.

I declared the,
array of pointers to functions (where each functions takes void as
argument and returns int) as follows:
int (*ptr [] ) (void); // Hope this is correct

Please help me to declare a pointer to above type.

Again many thanks for any help.....
 
R

Richard Heathfield

(e-mail address removed) said:
Guys,

I am sorry for asking so many questions.

I need to declare a pointer to an array of pointer to functions
where each function takes void as an argument and returns int.

typedef is your friend. Start off by setting up a synonym for the function
type:

typedef int junkyfunc(void);

Now let's have an array of pointers:

#define N 32 /* or whatever */

typedef junkyfunc *array_of_N_jf_ptrs[N];

Now we can easily define a pointer to such an array:

array_of_N_jf_ptrs *p;
 
M

Mark Bluemel

Richard said:
(e-mail address removed) said:
Guys,

I am sorry for asking so many questions.

I need to declare a pointer to an array of pointer to functions
where each function takes void as an argument and returns int.

typedef is your friend. Start off by setting up a synonym for the function
type:

typedef int junkyfunc(void);

Now let's have an array of pointers:

#define N 32 /* or whatever */

typedef junkyfunc *array_of_N_jf_ptrs[N];

Now we can easily define a pointer to such an array:

array_of_N_jf_ptrs *p;

Hmmm. Is it actually necessary, in most cases, to distinguish between a
a pointer to an array of such functions and a pointer to the first
function in such an array?
 
J

junky_fellow

(e-mail address removed) said:
I am sorry for asking so many questions.
I need to declare a pointer to an array of pointer to functions
where each function takes void as an argument and returns int.

typedef is your friend. Start off by setting up a synonym for the function
type:

typedef int junkyfunc(void);

Now let's have an array of pointers:

#define N 32 /* or whatever */

typedef junkyfunc *array_of_N_jf_ptrs[N];

Now we can easily define a pointer to such an array:

array_of_N_jf_ptrs *p;
Thanks Richard. This is really a nice way of declaring complex types.
 
J

junky_fellow

Richard said:
(e-mail address removed) said:
typedef is your friend. Start off by setting up a synonym for the function
type:
typedef int junkyfunc(void);
Now let's have an array of pointers:
#define N 32 /* or whatever */
typedef junkyfunc *array_of_N_jf_ptrs[N];
Now we can easily define a pointer to such an array:
array_of_N_jf_ptrs *p;

Hmmm. Is it actually necessary, in most cases, to distinguish between a
a pointer to an array of such functions and a pointer to the first
function in such an array?- Hide quoted text -

Actually, I need to find the number of elements in the array. So, I
must know its type. By having a pointer to first element, I cannot do
that.
 
K

Keith Thompson

Mark Bluemel said:
Richard said:
(e-mail address removed) said:
I need to declare a pointer to an array of pointer to functions
where each function takes void as an argument and returns int.
typedef is your friend. Start off by setting up a synonym for the
function type:
typedef int junkyfunc(void);
Now let's have an array of pointers:
#define N 32 /* or whatever */
typedef junkyfunc *array_of_N_jf_ptrs[N];
Now we can easily define a pointer to such an array:
array_of_N_jf_ptrs *p;

Hmmm. Is it actually necessary, in most cases, to distinguish between
a a pointer to an array of such functions and a pointer to the first
function in such an array?

Yes, it's necessary to distinguish between a pointer to an array and a
pointer to the first element of an array. The main difference is that
pointers to arrays are rarely useful.
 
K

Keith Thompson

Actually, I need to find the number of elements in the array. So, I
must know its type. By having a pointer to first element, I cannot do
that.

If you declare a pointer to an array, the size of the array must be
fixed at compilation time (unless you're using VLAs, which aren't
universally supported).

Pointers to arrays are rarely as useful as pointers to the first
element of an array. You can use ordinary indexing to access all the
elements of the array. You just have to keep track of the size
separately.
 
C

CBFalconer

Mark Bluemel said:
Richard said:
(e-mail address removed) said:
I need to declare a pointer to an array of pointer to functions
where each function takes void as an argument and returns int.
typedef is your friend. Start off by setting up a synonym for
the function type:
typedef int junkyfunc(void);
Now let's have an array of pointers:
#define N 32 /* or whatever */
typedef junkyfunc *array_of_N_jf_ptrs[N];
Now we can easily define a pointer to such an array:
array_of_N_jf_ptrs *p;

Hmmm. Is it actually necessary, in most cases, to distinguish
between a a pointer to an array of such functions and a pointer
to the first function in such an array?

Actually, I need to find the number of elements in the array. So,
I must know its type. By having a pointer to first element, I
cannot do that.

Sure you can. When you allocate memory to p with:
p = malloc(N * sizeof *p;
just remember the value of N. If you define a local variable for
the array, with:
arr_of_N_jf_ptrs arr[N];
you can either retain N or recover the value (in the same scope)
with "(sizeof arr)/sizeof arr[0])".
 
M

Mark Bluemel

Keith said:
Yes, it's necessary to distinguish between a pointer to an array and a
pointer to the first element of an array. The main difference is that
pointers to arrays are rarely useful.
Yes. I think that was what I was trying to express - but failed...
 
J

John Bode

Guys,

I am sorry for asking so many questions.

I need to declare a pointer to an array of pointer to functions
where each function takes void as an argument and returns int.

f -- f
*f -- is a pointer
(*f)[N] -- to an N-element array
*(*f)[N] -- of pointer
(*(*f)[N])() -- to function
(*(*f)[N])(void) -- taking void
int (*(*f)[N])(void) -- and returning int

Hope that helps. Typedefs would make it more readable; naked pointers are
a bit ugly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top