* How to prototype functions that return pointers
@ 2005-06-24 21:25 James Colannino
[not found] ` <op.ssv8s2eyizdgzp@mail.nairnconsulting.ca>
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: James Colannino @ 2005-06-24 21:25 UTC (permalink / raw)
To: linux-c-programming
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
--
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to prototype functions that return pointers
[not found] ` <op.ssv8s2eyizdgzp@mail.nairnconsulting.ca>
@ 2005-06-24 21:50 ` James Colannino
2005-06-25 6:22 ` wwp
0 siblings, 1 reply; 5+ messages in thread
From: James Colannino @ 2005-06-24 21:50 UTC (permalink / raw)
To: linux-c-programming
Richard Nairn wrote:
> 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
--
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to prototype functions that return pointers
2005-06-24 21:50 ` James Colannino
@ 2005-06-25 6:22 ` wwp
0 siblings, 0 replies; 5+ messages in thread
From: wwp @ 2005-06-25 6:22 UTC (permalink / raw)
To: linux-c-programming
Hello James,
On Fri, 24 Jun 2005 14:50:59 -0700 James Colannino <james@colannino.org>
wrote:
> Richard Nairn wrote:
> > 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,
--
wwp
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to prototype functions that return pointers
2005-06-24 21:25 How to prototype functions that return pointers James Colannino
[not found] ` <op.ssv8s2eyizdgzp@mail.nairnconsulting.ca>
@ 2005-06-25 6:56 ` Steve Graegert
2005-06-26 3:36 ` Glynn Clements
2 siblings, 0 replies; 5+ messages in thread
From: Steve Graegert @ 2005-06-25 6:56 UTC (permalink / raw)
To: James Colannino; +Cc: linux-c-programming
On 6/24/05, James Colannino <james@colannino.org> wrote:
> 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.
> 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 <graegerts@gmail.com> || <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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to prototype functions that return pointers
2005-06-24 21:25 How to prototype functions that return pointers James Colannino
[not found] ` <op.ssv8s2eyizdgzp@mail.nairnconsulting.ca>
2005-06-25 6:56 ` Steve Graegert
@ 2005-06-26 3:36 ` Glynn Clements
2 siblings, 0 replies; 5+ messages in thread
From: Glynn Clements @ 2005-06-26 3:36 UTC (permalink / raw)
To: James Colannino; +Cc: linux-c-programming
James Colannino wrote:
> 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);
> 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".
> 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:
> 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.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-06-26 3:36 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-06-24 21:25 How to prototype functions that return pointers James Colannino
[not found] ` <op.ssv8s2eyizdgzp@mail.nairnconsulting.ca>
2005-06-24 21:50 ` James Colannino
2005-06-25 6:22 ` wwp
2005-06-25 6:56 ` Steve Graegert
2005-06-26 3:36 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).