Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: declarator |
| Date | 2015-06-29 22:13 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <mmstuk$ldj$1@dont-email.me> (permalink) |
| References | <mmq8l7$qcm$1@dont-email.me> <87d20eesim.fsf@bsb.me.uk><mms93e$k2m$1@dont-email.me><p2c3pa578rsfhdkfq2olp0bb9vq710lfec@4ax.com><mmsocv$eqk$1@dont-email.me> <lnk2um2bv7.fsf@kst-u.example.com> |
"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnk2um2bv7.fsf@kst-u.example.com...
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>> Ok here is just a quickie. Demonstrating the "function pointer." This
>> is
>> sloppy because I did get these two warnings after compilation. And I know
>> if
>> something was passed to the print wrapper function and the printf
>> specifier
>> needed to be something other than %s there would be a problem. But this
>> is
>> as I say just a quickie.
>
> Compiler warnings often indicate *serious* problems. That's not always
> the case, but it is here.
>
>> #include <stdio.h>
>>
>> int print(char *);
>> int (*pf) (char *);
>
> Ok, pf is a pointer-to-function.
>
>> int main()
>> {
>> pf = print("hello there");
>
> The print() function return a result of type int. You're trying to
> assign that int value to a function pointer.
>
> The name of a function, in an expression context, gives you the address
> of (i.e., a pointer to) the function. The name of a function followed
> by parentheses *calls* the function, and gives you the value returned by
> the function.
>
> If you want the pointer object pf to point to the function print, you
> need to do:
>
> pf = print;
>
> which is equivalent to:
>
> pf = &print;
Of the two above is there a preferred format?
> Once you've done that, you can use pf to call the function it points to:
>
> pf("hello there");
Ah hah. I guess I am trying to declare a "pointer to a function" without
even knowing what it looks like in code and what it's for. Whew. But I am
seeing.
> which is equivalent to:
>
> (*pf)("hello there");
Ok now the above. Is that the way you would want to use this pointer
function if it wasn't assigned? ie
pf=print;
>> return pf;
>
> Now you're trying to return a pointer from main. Why?
I don't know. I have no idea what Im doing. Til now as you have
exaplained it to me. I'm not quite sure in what circustance one would want
to use a pointer to function.
>> }
>>
>> int print(char *input)
>> {
>> printf("%s\n", input);
>> return 0;
>> }
>>
>> And the warnings:
>>
>> p.c: In function 'main':
>> p.c:8:8: warning: assignment makes pointer from integer without a cast
>> [enabled by default]
>> pf = print("hello there");
>> ^
>> p.c:9:5: warning: return makes integer from pointer without a cast
>> [enabled
>> by default]
>> return pf;
>> ^
>>
>> I did not use -Wall either. as the compiler says "enabled by default. It
>> compiles and runs.
>
> gcc has an unfortunate habit of issuing non-fatal warnings for code that
> is unambiguously incorrect.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
declarator "Bill Cunningham" <nospam@nspam.invalid> - 2015-06-28 21:58 -0400
declarator "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-06-28 19:14 -0700
Re: declarator "Bill Cunningham" <nospam@nspam.invalid> - 2015-06-28 22:36 -0400
Re: declarator "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-06-28 19:46 -0700
Re: declarator Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-06-29 11:22 +0100
Re: declarator "Bill Cunningham" <nospam@nspam.invalid> - 2015-06-29 16:07 -0400
Re: declarator "Bill Cunningham" <nospam@nspam.invalid> - 2015-06-29 16:09 -0400
Re: declarator Barry Schwarz <schwarzb@dqel.com> - 2015-06-28 21:51 -0700
Re: declarator Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-06-29 11:08 +0100
Re: declarator "Bill Cunningham" <nospam@nspam.invalid> - 2015-06-29 16:18 -0400
Re: declarator Barry Schwarz <schwarzb@dqel.com> - 2015-06-29 14:23 -0700
Re: declarator "Bill Cunningham" <nospam@nspam.invalid> - 2015-06-29 18:39 -0400
Re: declarator "Bill Cunningham" <nospam@nspam.invalid> - 2015-06-29 20:39 -0400
Re: declarator Keith Thompson <kst-u@mib.org> - 2015-06-29 19:01 -0700
Re: declarator "Bill Cunningham" <nospam@nspam.invalid> - 2015-06-29 22:13 -0400
Re: declarator Barry Schwarz <schwarzb@dqel.com> - 2015-06-29 20:45 -0700
Re: declarator Keith Thompson <kst-u@mib.org> - 2015-06-30 08:22 -0700
Re: declarator Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 08:44 -0700
Re: declarator "Bill Cunningham" <nospam@nspam.invalid> - 2015-06-30 12:48 -0400
Re: declarator Barry Schwarz <schwarzb@dqel.com> - 2015-06-30 15:32 -0700
Re: declarator John Bode <jfbode1029@gmail.com> - 2015-07-01 13:50 -0700
Re: declarator gazelle@shell.xmission.com (Kenny McCormack) - 2015-07-01 21:57 +0000
Re: declarator "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-01 15:05 -0700
Re: declarator "Bill Cunningham" <nospam@nspam.invalid> - 2015-07-02 20:34 -0400
Re: declarator Jens Stuckelberger <Jens_Stuckelberger@nowhere.net> - 2015-06-29 14:55 +0000
csiph-web