Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does (int) mean in C programming

Tags:

c

void problem3(void) {
    int overflowme[16];
    int x = (int) problem3; // x is the address of the first instr for problem3
    printf("hello world\n");
    overflowme[17] = x; 

I'm wondering what does the (int) do in C programming.

like image 438
user133466 Avatar asked Nov 30 '22 19:11

user133466


2 Answers

It's a typecast, and tells the compiler "Ignore the type that problem3 really has, and deal with it as if it were typed as an int".

In this example, problem3 has a function pointer type, so normally the compiler would reject the program (Using a function pointer when an integer is expected is normally a programmer error). The typecast forces a different interpretation - the programmer is stepping in and saying "I know what I'm doing".

like image 156
Adam Wright Avatar answered Dec 02 '22 08:12

Adam Wright


It's an explicit cast. You are casting the value of problem3 to an integer and then assigning that integer value to x.

Note that this does not actually change the value of problem3.

like image 42
Matthew Jones Avatar answered Dec 02 '22 08:12

Matthew Jones