I've heard some mention "anonymous" objects in c.l.c and other places
(conversations), and I was wondering what exactly these are. Anonymous
seems to imply that there is no name, but no name implies that one
can't access the object, which implies that the object would be
useless.
So what exactly are they?
There are two things that can be considered anonymous objects in C.
The first are string literals, both normal and wide.
In the canonical C program:
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
The string literal "Hello, World\n", is most certainly an object, in
fact it is an array of 14 characters, including the '\0' at the end,
but it has no name.
Likewise:
char *cp = "A string literal";
This string literal has no name either, yet can be accessed from many
different places in a program, both where the declaration is in scope
and any place where cp might be passed as an argument. The pointer
has a name, the object it points to does not.
The other case of what might or might not be called anonymous objects
in C are objects "imposed" on dynamically allocated memory. This is
not really the same thing, but since the term is not defined by the C
standard, you could stretch the point.
Consider:
struct anon { int x; double y; long z; };
Then later in code:
struct anon *ap = malloc(sizeof *ap);
Assuming the malloc() succeeds, once you actually assign a value to
any or all of ap->x, ap->y, or ap->z, that block of memory takes on
the effective type of a struct anon, although technically it is not
actually a struct anon object.