top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can a pointer point to itself?

+2 votes

Explain With An Example

posted Dec 22, 2014 by Chirag Gangdev

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote
Best answer

Hope I am able to understand the question correctly -

The answer is no.

Suppose you try then the possible way would be something like

int *foo = &foo;

But the types would be incompatible as &foo has type int**.

answer Dec 23, 2014 by Salil Agrawal
0 votes
eg 1

int x;
int *ptr = &x; This is correct since an integer pointer is used to store the address of an integer.

eg 2

int *ptr = &ptr; This is not a safe way of programming. Its an incompatible pointer. It's purpose is to store the address of an integer not a pointer. To store the address of a pointer, you need a DOUBLE pointer.

What are you trying to achieve by the way?

answer Dec 23, 2014 by Ankush Surelia
I want ptr and &ptr both should print same value(as pointer is pointing to itself)
If i do as per your code, I am getting 2 warnings and also unxpected o/p.
0 votes

A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items. Pointers are very useful for another type of parameter passing, usually referred to as Pass By Address. Pointers are essential for dynamic memory allocation.

answer Dec 27, 2014 by Leon
Similar Questions
+4 votes

What is the point of declaring Pointer of different types (eg. integer,float,char) as we all know pointer takes 4 bytes of space regardless which type of pointer it is and only contains address?