complex declarations

N

noor.fatma

Hi,


recently I attended a test on C where I was asked to declare the
following declarations in one line.

void ** ( *p )(int *, char *);
int *a;

how should i do this shall i use typedef for it.

Thanks,
 
I

Ian Collins

Hi,


recently I attended a test on C where I was asked to declare the
following declarations in one line.

void ** ( *p )(int *, char *);
int *a;
Why? The question doesn't make sense.
 
S

santosh

Hi,


recently I attended a test on C where I was asked to declare the
following declarations in one line.

void ** ( *p )(int *, char *);
int *a;

how should i do this shall i use typedef for it.

It not clear what you were asked to do. Write out the declarations in
English or concactenate them into a single line...

Maybe you can clarify further?
 
N

noor.fatma

for example
we can declare two variables as:
int i;
int j;

and also we can combine them to form a single line of declaration
int i,j;

in the similar way.
 
M

michaelquinlivan

void ** ( *p )(int *, char *); int *a;

at least, gcc allows you to do this, but it sounds to me that you have
got the question mixed up/misinterpreted. Surely no examiner would ask
such a stupid question...
 
M

michaelquinlivan

void ** ( *p )(int *, char *); int *a;

at least, gcc allows you to do this, but it sounds to me that you have
got the question mixed up/misinterpreted. Surely no examiner would ask
such a stupid question...
 
N

noor.fatma

I worked out the answer
we can write the declaration as
void **(*p)(int *,char *), *a;

now later when we use a we can typecast it to int *.
It will work without typecasting as well.
I tried it out. it works.

Thanks
 
I

Ian Collins

(e-mail address removed) wrote:

Please don't top post.
for example
we can declare two variables as:
int i;
int j;

and also we can combine them to form a single line of declaration
int i,j;
Which many (including me) consider very bad style.

I still don't see how this relates to the question, why would you want
to put a function pointer and an int pointer declaration on one line?
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Hi,


recently I attended a test on C where I was asked to declare the
following declarations in one line.

void ** ( *p )(int *, char *);
int *a;

how should i do this shall i use typedef for it.

void ** ( *p )(int *, char *); int *a;
 
S

santosh

for example
we can declare two variables as:
int i;
int j;

and also we can combine them to form a single line of declaration
int i,j;

in the similar way.

If what you say is correct, then it's certainly a stupid question!

Just combine them into a line like this:

void ** (*p)(int *, char *); int *a;
 
G

Guest

I worked out the answer
we can write the declaration as
void **(*p)(int *,char *), *a;

You changed the declaration. You were asked to declare a as pointer to
int, according to your own original post, and your solution declares it
as pointer to void.
now later when we use a we can typecast it to int *.
It will work without typecasting as well.
I tried it out. it works.

No, it doesn't. The original declaration allows you to write *a = 0;.
Your modified declaration does not. It's possible that what you wrote
is the intended solution, but if that's the case, the question was
simply plain wrong.
 
C

Chris Dollin

I worked out the answer
we can write the declaration as
void **(*p)(int *,char *), *a;

now later when we use a we can typecast it to int *.
It will work without typecasting as well.
I tried it out. it works.

This is so gobsmackingly awful an idea that I urge you
to forget it immediately and read K&R2.

If that's what the people who were testing you /wanted/
my advice would be to run away, very fast, very far, very
soon.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top