D
dough
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?
itself? Why or why not?
dough said:Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?
Sure, but of what use would it be?
Walter said:I suspect that 'dough' means "in a single statement". The use of it
would be to answer homework questions.
I have a solution that compiles and executes without problem with
any warning level I throw at it.
It does raise some subtle language points.
The point it raises is this: may an initializer for a variable referenceWalter said:I suspect that 'dough' means "in a single statement". The use of it
would be to answer homework questions.
I have a solution that compiles and executes without problem with
any warning level I throw at it. It does raise some subtle language
points.
actually no. The types would be incompatible.tedu said:Sure, but of what use would it be?
Nils said:actually no. The types would be incompatible.
You can't do int *foo = &foo; &foo has type int**.
I am confused. Why would you think it is undefined? The scope of a
variable starts immediately after the declarator is done, it has
nothing to do with initialization. int j = j + 1 and int j[j] are
undefined due to different reasons; in fact, one can contrive
situations where the latter statement is completely well-defined.
I am confused. Why would you think it is undefined? The scope of a
variable starts immediately after the declarator is done, it has
nothing to do with initialization. int j = j + 1 and int j[j] are
undefined due to different reasons; in fact, one can contrive
situations where the latter statement is completely well-defined.
Nils said:actually no. The types would be incompatible.
You can't do int *foo = &foo; &foo has type int**.
dough said:Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?
Flash said:Actually, if you pick the correct types, you don't have a problem with
types and pointers to pointers.
I know> Skarmander restored context when he copied:I am confused. Why would you think it is undefined? The scope of aThe point it raises is this: may an initializer for a variable
reference the address of that variable?
The point is fairly academic, since it's hard to imagine a
situation where this is both useful and not rewritable as
initialization after declaration. Note that we're not talking about
nonsense like
int j = j + 1;
which is undefined in any case since j is used uninitialized. But
how about
ptrdiff_t y = &y - &y;
>>
variable starts immediately after the declarator is done, it has
nothing to do with initialization. int j = j + 1 and int j[j] are
undefined due to different reasons; in fact, one can contrive
situations where the latter statement is completely well-defined.
Skarmander said:I knowSkarmander restored context when he copied:I am confused. Why would you think it is undefined? The scope of aThe point it raises is this: may an initializer for a variable
reference the address of that variable?
The point is fairly academic, since it's hard to imagine a
situation where this is both useful and not rewritable as
initialization after declaration. Note that we're not talking about
nonsense like
int j = j + 1;
which is undefined in any case since j is used uninitialized. But
how about
ptrdiff_t y = &y - &y;
variable starts immediately after the declarator is done, it has
nothing to do with initialization. int j = j + 1 and int j[j] are
undefined due to different reasons; in fact, one can contrive
situations where the latter statement is completely well-defined.
int j = j + 1;
is undefined for a different reason. I explicitly said it wasn't what we
were talking about (but now we are
I also know that in turn is completely different from
int j[j];
which involves getting tricky with overloading. This is unlike
everything else we've discussed because there are two distinct 'j's here.
This still doesn't answer my question, because that asks about the scope
of a variable *while in the initialization part of the declaration*. If
you happen to know that's defined, great. Quote the relevant parts of
the standard that imply this, please. (But like I said, the point is
fairly academic, so you won't see me crying if this is forever left
unresolved.)
Lawrence said:However there is no type for which this works. If your pointer have type
TYPE * then a pointer to it has type TYPE **. Since an object in C can
only have one type it can't be both TYPE * and TYPE ** at once.
However there is a similar question for which C does have an answer: "Is
it possible in C to declare and initialize a pointer that points to
THE FIRST BYTE of itself?"
That can be done by for example
char *ptr = (char *)&ptr;
or perhaps
void *ptr = &ptr;
A pointer to the pointer object can be recreated from these by converting
the value back to the correct type (char ** and void ** respectively).
But you can change int for another type which makes it work.
pete said:/* BEGIN new.c */
#include <stdio.h>
int main(void)
{
void *pointer = &pointer;
if (pointer == &pointer) {
printf("&pointer is %p\n", pointer);
printf("&pointer is %p\n", (void *)&pointer);
}
return 0;
}
/* END new.c */
Villy said:"After the declarator", is that at the following semicolon or comma,
or immediately after the name itself but before a possible equals sign?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.