0
static int* p= (int*)(&foo);

I just know p points to a memory in the code segment. But I don't know what exactly happens in this line.
I thought maybe it's a pointer to a function but the format to point a function is:

returnType (*pointerName) (params,...);
pointerName = &someFunc; // or pointerName=someFunc;
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
TheLogicGuy
  • 682
  • 8
  • 19

2 Answers2

1

You take the address of foo and cast it to pointer to int.

If foo and p are of different types, the compiler might issue a warning about type mismatch. The cast is to supress that warning.

For example, consider the following code, which causes a warning from the compiler (initialization from incompatible pointer type):

float foo = 42;
int *p = &foo;

Here foo is a float, while p points to an int. Clearly - different types.

A typecasting makes the compiler treat one variable as if it was of different type. You typecast by putting new type name in parenthesis. Here we will make pointer to float to be treated like a pointer to int and the warning will be no more:

float foo = 5;
int *p = (int*)(&foo);

You could've omitted one pair of parenthesis as well and it'd mean the same:

float foo = 5;
int *p = (int*)&foo;

The issue is the same if foo is a function. We have a pointer to a function on right side of assignment and a pointer to int on left side. A cast would be added to make a pointer to function to be treated as an address of int.

lukeg
  • 4,189
  • 3
  • 19
  • 40
  • By type of a function you mean the type of the returned variable? – TheLogicGuy Jan 21 '17 at 11:03
  • There are no functions there! I've expanded my answer so it's probably clearer now. – lukeg Jan 21 '17 at 11:11
  • But consider `foo` is a function not a variable. How is it possible now? – TheLogicGuy Jan 21 '17 at 11:19
  • I expanded my answer even more. The problem is again type mismatch. – lukeg Jan 21 '17 at 12:02
  • Can I always cast between two pointers? Even if I cast to long* from int* for example? I tested some cases and never have gotten a runtime error because of wrong castings (long to int, long to char) I just got some crazy results and sometimes random ones. How is it possible to cast without an error? – TheLogicGuy Jan 21 '17 at 12:37
  • I mean what happens when I cast and there are illegal bits that cannot be casted to the desired type – TheLogicGuy Jan 21 '17 at 12:44
  • 1
    See http://stackoverflow.com/questions/4810417/c-when-is-casting-between-pointer-types-not-undefined-behavior – lukeg Jan 21 '17 at 13:12
0

A pointer of a type which points to an object (i.e. not void* and not a pointer to a function) cannot be stored to a pointer to any other kind of object without a cast, except in a few cases where the types are identical except for qualifiers. Conforming compilers are required to issue a diagnostic if that rule is broken.

Beyond that, the Standard allows compilers to interpret code that casts pointers in nonsensical fashion unless code aides by some restrictions which, as written make such casts essentially useless, for the nominal purpose of promoting optimization. When the rules were written, most compilers would probably do about half of the optimizations that would be allowed under the rules, but would still process pointer casts sensibly since doing so would cost maybe 5% of the theoretically-possible optimizations. Today, however, it is more fashionable for compiler writers to seek out all cases where an optimization would be allowed by the Standard without regard for whether they make sense.

Compilers like gcc have an option -fno-strict-aliasing that blocks this kind of optimization, both in cases where it would offer big benefits and little risk, as well as in the cases where it would almost certainly break code and be unlikely to offer any real benefit. It would be helpful if they had an option to block only the latter, but I'm unaware of one. Thus, my recommendation is that unless one wants to program in a very limited subset of Dennis Ritchie's language, I'd suggest targeting the -fno-strict-aliasing dialect.

supercat
  • 77,689
  • 9
  • 166
  • 211