Why is it ok to cast to an undeclared struct pointer in C?

Back

Why does this code compile? struct test is not even forward declared.

int main(){
 *(int*)((struct test*) 0x1234) = 0x0;
 return 0;
}
Saksham Agrawal
5/27/2025, 3:45:49 AM

Answers (3)

The appearance of struct test* is an implicit declaration of struct test. Specifically, struct test declares the type. Because the struct type is declared a pointer to that type is valid. Even though the struct type was not fully defined, the representation of the pointer type is known since a pointer to any struct type has the same representation as described in section 6.2.5p33 of the C standard:

2
Saksham Agrawal
5/27/2025, 3:46:16 AM

any suggestions on it ?

1
Saksham Agrawal
5/27/2025, 5:16:06 PM

him

0
Saksham Agrawal
10/10/2025, 9:42:32 AM

Your Answer

You need to be logged in to answer this question. Login or Register to continue.