Pointer declarations ane meaning

I need help interpretting these declarations for a homework assignment. No lecture given. And i'm having trouble googling this up.

float (*x)(int *a);
(b) float (*x(int *a))[20];
(c) float x(int (*a)[]);
(d) float x(int *a[]);
(e) float *x(int a[]);
(f) float *x(int (*a)[]);
(g) float *x(int *a[]);
(h) float (*x)(int (*a)[]);
(i) float *(*x)(int *a[]);
(j) float (*x[20])(int a);
(k) float *(*x[20])(int *a);
look up function pointer syntax.

I believe, for example, the first one is a pointer to a function that takes an int pointer argument and returns a type of float. The others are just adding onto this, if I got it right.

This may help: https://www.geeksforgeeks.org/complicated-declarations-in-c/
Last edited on
C's declarators are designed to be suggestive of usage. For example, given the declaration
float *x;
The syntax is suggesting that the expression *x has type float.
Similarly, given the declaration (a)
float (*x)(int *a);
The syntax is suggesting that the expression (*x) has type float(int*) (it does) and therefore that (*x)(some_pointer_to_int) has type float.

Hopefully I didn't make any typos while typing this out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* (a) */ float (*x)(int *a);
/* Declare x as pointer to function returning a float and
   accepting one argument of type int*. */

/* (b) */ float (*x(int *a))[20];
/* Declare x as a function returning a pointer to array of
   20 elements of type float and accepting one argument of type
   int*. */

/* (c) */ float x(int (*a)[]);
/* Declare x as a function returning float and accepting one argument of type int (*a)[]
   int(*)[] is a pointer to array of unknown bound with an element type of int. 

   This is not valid C++.  It is valid C.
*/

/* (d) */ float x(int *a[]);
/* Declare x as a function returning float and accepting one argument of type int**. */

/* (e) */ float *x(int a[]);
/* Declare x as a function returning a pointer to float and accepting
   one argument of type int*. */

/* (f) */ float *x(int (*a)[]); 	/* Like (c), but returning float* instead. */
/* (g) */ float *x(int *a[]); 		/* Like (d), but returning float* instead. */
/* (h) */ float (*x)(int (*a)[]); 	/* Like (c), but x is a pointer-to-function instead. */
/* (i) */ float *(*x)(int *a[]);        /* Like (g), but x is a pointer-to-function instead. */
/* (j) */ float (*x[20])(int a);
/* Declare x as a array of 20 elements with type float(*)(int).
   float(*)(int) is the type of a pointer to function returning float and accepting one argument of type int.  */

/* (k) */ float *(*x[20])(int *a);      /* Like (j), but the element type of the array is float*(*)(int*). */


There is also https://cdecl.org/ if you need it.
Last edited on
Topic archived. No new replies allowed.