Skip to contentSkip to conversation

How to prototype functions that return pointers

DISCUSSION James Colannino 4 participants 5 messages Jun 24, 2005 – Jun 26, 2005 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Ok, so this is a very embarassing question. I tried googling for the answer but haven't figured it out yet, and everything I've tried results in compilation errors. I want to prototype a function that takes nothing as an argument but returns a pointer to a character.

I've tried the following:

char * readline();
char *readline();

These first two result in 4 errors, each one identical and each one telling me "error: two or more data types in declaration of 'readline' "

*char readline();
* char readline();

These second two result in "error: syntax error before 'char' "

I know this should be so simple, but I can't for the life of me figure this out. Thanks very much in advance.

James

Signature · 5 lines
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

" Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety." --Benjamin Franklin

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 3 lines · Richard Nairn

Believe the correct decleration is (char *) readline();

Order of ops.

That was the next one I tried. That yielded a syntax error. I found out though that the "two or more data types" error was the result of a more difficult bug to find that had to do with something in another header file. Once I fixed that, the declaration char * readline(); worked. Needless to say I'm very embarassed now... :-P

James

Signature · 5 lines
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

" Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety." --Benjamin Franklin
#3 lore raw

Hello James,

Quoted 10 lines · On Fri, 24 Jun 2005 14:50:59 -0700 James Colannino <[email protected]> wrote:
Quoted 3 lines · Richard Nairn

Believe the correct decleration is (char *) readline();

Order of ops.

That was the next one I tried. That yielded a syntax error. I found out though that the "two or more data types" error was the result of a more difficult bug to find that had to do with something in another header file. Once I fixed that, the declaration char * readline(); worked. Needless to say I'm very embarassed now... :-P

Moreover, the following ones are (should be) identical towards the C parser, spaces between those tokens are not mandatory or can be repeated, and depend on the coding style and recommendation/standard you follow:

char * readline();
char *readline();

Regards,

Signature · 1 line
wwp
#4 lore raw
In reply to James Colannino
Quoted 9 lines · James Colannino

Ok, so this is a very embarassing question. I tried googling for the answer but haven't figured it out yet, and everything I've tried results in compilation errors. I want to prototype a function that takes nothing as an argument but returns a pointer to a character.

I've tried the following:

char * readline();
char *readline();

I can't see a problem with this declaration, thus I am not able to reproduce the error(s). What I see here is a function taking an arbitrary number of arguments returning a pointer to a char.

Quoted 5 lines

These first two result in 4 errors, each one identical and each one telling me "error: two or more data types in declaration of 'readline' "

*char readline();
* char readline();

This is obviously wrong because you're trying to dereference a data type, not an identifier. Everything else that follows in this line is discarded by the parser.

I have compiled three different function declarations that all have a different meaning to help you understand what could have been wrong:

/* pointer to func() returning a char */
char (*func)();

/* function taking a char pointer and returning char-pointer */ char* f(char *);

/*
* pointer to a function taking pointer to a char,
* returning a pointer to a char
*/
char* (*fp)(char *);

Hope this clarifies the relations between pointers and functions and their arguments.

Kind Regards

\Steve

--

Steve Graegert <[email protected]> || <http://www.technologies.de/~sg/>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176) 21 24 88 69
Office: +49 (9131) 71 26 40 9

#5 lore raw
In reply to James Colannino
Quoted 4 lines · James Colannino

Ok, so this is a very embarassing question. I tried googling for the answer but haven't figured it out yet, and everything I've tried results in compilation errors. I want to prototype a function that takes nothing as an argument but returns a pointer to a character.

	char *myfunc(void);
Quoted 4 lines

I've tried the following:

char * readline();
char *readline();

Both of these declare a function taking unspecified arguments and returning a char*. If the function takes no arguments, the argument list should be declared as "void".

Quoted 2 lines

These first two result in 4 errors, each one identical and each one telling me "error: two or more data types in declaration of 'readline' "

That error isn't related to your prototypes, but the context in which they occur. In your follow-up, you note:

Quoted 4 lines

I found
out though that the "two or more data types" error was the result of a more difficult bug to find that had to do with something in another header file.

That seems likely.

Signature · 1 line
Glynn Clements <[email protected]>