Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #163265
| From | Manfred <noname@add.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: on confusing procedure names with the address of procedures |
| Date | 2021-10-30 05:32 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <sliebn$16l6$1@gioia.aioe.org> (permalink) |
| References | <86bl37e49v.fsf@levado.to> |
On 10/30/2021 2:37 AM, Meredith Montgomery wrote:
> I wrote
>
> --8<---------------cut here---------------start------------->8---
> int main()
> {
> char c1;
>
> printf("c1 is at %p\n", &c1);
> printf("and the addr of the addr of c1 is at %p\n", &&c1);
> }
> --8<---------------cut here---------------end--------------->8---
>
> and I didn't this to make any sense. Indeed, I get
>
> --8<---------------cut here---------------start------------->8---
> %make nonsense
> gcc nonsense.c -o nonsense
> nonsense.c: In function 'main':
> nonsense.c:12:3: error: label 'c1' used but not defined
> 12 | printf("c1 is at %p\n", &&c1);
> | ^~~~~~
> make: *** [<builtin>: nonsense] Error 1
> %
> --8<---------------cut here---------------end--------------->8---
>
> The error message totally lost me.
>
> But then I wrote
>
> --8<---------------cut here---------------start------------->8---
> #include <stdio.h>
> int main()
> {
> printf("main is at %p\n", main);
> printf("main is at %p\n", &main);
> }
> --8<---------------cut here---------------end--------------->8---
>
> and I expected a compilation error of some sort, but I got
>
> --8<---------------cut here---------------start------------->8---
> %CFLAGS=-Wall make main-addr
> gcc -Wall main-addr.c -o main-addr
>
> %./main-addr.exe
> main is at 00007ff617431594
> main is at 00007ff617431594
> %
> --8<---------------cut here---------------end--------------->8---
>
> So main must be stored at 00007ff617431594 and so it becomes unintuitive
> that getting the address of main yields the same value.
In expressions where the function name is used (but not as a call to
that function, so not main()), the function is automatically converted
to a pointer to function (similar to arrays that decay to pointers in
many circumstances). So using main and &main as argument to printf
indeed yields the same result: the address of main.
The same applies to the inverse case, if you have a pointer to function:
#include <stdio.h>
void bar()
{
printf("hello\n");
}
int main()
{
void (*foo)() = bar;
// You can execute the call in both of the following ways:
foo(); // the pointer is implicitly dereferenced
(*foo)(); // the pointer is explicitly dereferenced
}
>
> My interpretation of this last program is that
>
> main = &main
>
> but for the first program
>
> &c1 =/= & &c1
>
> because the right side is nonsensical? It's trying to get the address
> of an address, when we should only get the address of a name. Can't get
> the address of numbers.
As a rationale, addresses represent memory locations (for functions it
is a bit more complicated than that, so let's stick to data for this
second part). This means that an address may be taken from objects that
have some memory storage, e.g. c1.
The result of &c1 is indeed a pure value which has no memory storage, so
its address cannot be taken (in fact it may be physically stored in a
register and not in any memory location, but even if it were stored
somewhere in memory, you would miss some identification of that memory
location in order to get its address).
>
> Pretty confusing.
>
Yet pointers and addresses are probably amongst the most powerful
features of C.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
on confusing procedure names with the address of procedures Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 21:37 -0300
Re: on confusing procedure names with the address of procedures Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-29 18:29 -0700
Re: on confusing procedure names with the address of procedures Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-29 18:33 -0700
Re: on confusing procedure names with the address of procedures Manfred <noname@add.invalid> - 2021-10-30 05:32 +0200
Re: on confusing procedure names with the address of procedures James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-30 01:26 -0400
Re: on confusing procedure names with the address of procedures Manfred <noname@add.invalid> - 2021-10-30 19:05 +0200
Re: on confusing procedure names with the address of procedures James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-30 15:17 -0400
Re: on confusing procedure names with the address of procedures Bart <bc@freeuk.com> - 2021-10-30 11:36 +0100
Re: on confusing procedure names with the address of procedures Noob <root@127.0.0.1> - 2021-11-01 13:03 +0100
csiph-web