-3

if c is of float type and *p is a pointer to c. * p should also be of float type. but it **p2 is a pointer to *p . should it be an int type??because all the " * " says is that " value at adress " is of int type (in case of **p2) like :

float s=10.50 ; 
float *p ;
*p=&s ;
float **p2=&(*p);

Should the above line be float or an int type since the value stored at *p is the address of s(which is an integer)?

merlin2011
  • 71,677
  • 44
  • 195
  • 329
julia
  • 1
  • 1
  • 9
    A pointer to a pointer is a pointer. It is not an `int`. – juanchopanza Feb 08 '15 at 21:32
  • Unless you're addressing dereferenced iterators in C++, `&(*anything)` is a near-guaranteed sign the wheels fell off. That should be `float **p2 = &p;`. the prior line is just-as-wrong. You seem to be of the mind that a pointer cannot appear in an expression without an asterisk. – WhozCraig Feb 08 '15 at 21:33
  • 2
    Pointers are not integers. Pointers are not numbers. Pointers are pointers. – Keith Thompson Feb 08 '15 at 21:34
  • 2
    `*p=&s` is wrong. Maybe you meant `p=&s`. Then, `&(*p)` is not a `float **`. Maybe you meant `float **p2=&p;`? – Sourav Ghosh Feb 08 '15 at 21:35
  • 1
    Third line looks suspicious. You are storing value of s in address pointed by p. Shouldn't it be 'p=&s'. And pointers could be of same size of int. But pointers are not integers – Milind Dumbare Feb 08 '15 at 21:36
  • Possible duplicate of [What is the difference between float pointer and int pointer address?](http://stackoverflow.com/questions/15790980/what-is-the-difference-between-float-pointer-and-int-pointer-address) – jww Feb 08 '15 at 21:54

3 Answers3

0

One mistake that you are making is that you are dereferencing a pointer when you have the command

float **p2=&(*p);

Instead, what you want to write down there is:

float **p2=&p;

so that you can have a pointer to a pointer of a float.

MightyMouse
  • 13,208
  • 8
  • 33
  • 43
0

A pointer is an address in memory. And the address could be of float variable, an int variable or a structure of many such variables. But pointer is always pointer, not int or anything else. It could be of same size as integer on some architectures.

So p2 here is a pointer not float. And even p is a pointer.

Lets analyze the code:

 *p=&s;

Though this will give compiler warnings(incompatible assignment), you are copying address of s to place pointed by p. p here could be garbage and you are potentially copying address of s to some garbage address.

Address of s could be of bigger size than int. size of int is 4 byte and address of s(int) could be 8 byte. (On some architectures)

float **p2=&(*p);

Above line as same as float **p2 = p;

So p2 has same value as p which is some garbage uninitialized address.

Milind Dumbare
  • 3,104
  • 2
  • 19
  • 32
0

The asterisks are part of the C type. So the three variables you've defined are of types:

(float)
(float *)
(float **)

Each star means an extra level of indirection or a pointer. The 'float' part always means the thing you end up with after unravelling the pointers. The fact that the CPU might see pointers as an 'int-like' type isn't relevant to how this works.

user3710044
  • 2,261
  • 15
  • 15