beth = ted; // beth equal to ted ( 1776 )
beth = *ted; // beth equal to value pointed by ted ( 25 )

beth = &ted; // is this statement result in a compiler error? if beth is an int variable

7 Years
Discussion Span
Last Post by ravenous

It's easier if you put the types of the variables too. From what you have above, I think that you have this:

int value = 25;     // A variable that you implied was equal to 25
int *beth;          // A pointer to an integer
int *ted = &value;  // A pointer to an integer, initialised to the address of value

beth = ted;         // This is OK, they're both pointers
beth = *ted;        // This isn't OK, a pointer stores a memory address, not an int value
beth = &ted;        // This isn't OK, a "pointer to a pointer" is not the same as a "pointer to a double"

Also, use "code" tags :o)

Edited by ravenous: n/a


I thing program will be compile but can not direct assign the address of variable .

The two lines that I indicated as not OK should produce compile-time errors, so the program shouldn't compile. From the compiler's point of view, on line 6, you're trying to assign a variable of type int to one of type int * , so this shouldn't be allowed. And, on line 7, you're trying to assign a variable of type int ** to one of int * , so this shouldn't be allowed either.

I don't know how consistent different compilers are with this kind of implicit type-casting, but I can't imagine it's a good idea. If you need to make an assignment like this, then you can explicitly specify the type-cast, as in

beth = (int *)(&ted);

It will compile, but I'm not sure if it will produce a well-defined result?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.