Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer questions in c

Quick question about C programming. I haven't taken a course on it, but was reading online about pointers and the various notations used. Does the following line result in an array of 10 pointers to integers, or a pointer to an array of 10 ints?

int *x[10];

Also does the following result in a function that takes a double as input and returns a pointer to a character?

char (*f) (double a);

Thanks in advance!

Edit: Typed the first line of code incorrectly.

like image 752
user3421751 Avatar asked Jan 10 '23 17:01

user3421751


2 Answers

A function that takes a double as input and returns a char pointer would look like this:

char * foo(double arg);

Where arg is your input double, foo is the name of the function, and char * is the return type, a char pointer.

char (*foo)(double arg);

Declares a function pointer to a function that takes a double (arg) and returns a char.


Your attempt at creating an array is missing a variable name. And there are two different ways to do this...

int * array[10];

This declares an array with 10 indexes that holds int *, int pointers.

int (*array)[10];

This declares a pointer to an array of ints with size of 10.

like image 141
nhgrif Avatar answered Jan 20 '23 15:01

nhgrif


int *x[10]; means that x is an array with 10 elements, each of which is a pointer to int.

char (*f) (double a); means that f is a pointer to a function that returns char and has one parameter of type double.

like image 38
M.M Avatar answered Jan 20 '23 15:01

M.M