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

Recommended Answers

All 4 Replies

Compile it. My guess is the compiler won't automatically cast the address of that pointer to an integer value.

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)

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

commented: Spammer -1

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.