c - Returning address of a local variable error -


i typed code:

void main() {     int *a;     = foo();     printf("%d\n", *a); }  int* foo() {     int b = 10;     return &b; } 

after compiling, there 2 problems:

1. error - conflicting type foo() 2. warning - function returns address of local variable 

but write

int* foo(); void main() {     int *a;     = foo();     printf("%d\n", *a); }  int* foo() {     int b = 10;     return &b; } 

now, not give error after compiling, obvious, but, why compiler not give warning returning address of local variable?

how declaring or not declaring function affects returning address of local variable?

sorry not mentioning before, using gnu gcc compiler

the c standard not require compilers give warnings except syntax errors.

iso/iec 9899:1999, 5.1.1.3:

a conforming implementation shall produce @ least 1 diagnostic message (identified in implementation-defined manner) if preprocessing translation unit or translation unit contains violation of syntax rule or constraint, if behavior explicitly specified undefined or implementation-defined. diagnostic messages need not produced in other circumstances.

the behavior described inconsistent (i.e. not nice) valid/standard-compliant.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -