-1

address of a variable is assigned to integer variable without an error(just a warning) in C Language.how is this possible?? Can Anyone explain? Code:

int main()
{
    int a=9;
    int *x=&a;
    int y=&a;
    printf("%d\n",y);
    printf("%p",x);
    return 0;
}

OUTPUT of the code:

test.c: In function 'main':
test.c:7:11: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
       int y=&a;
           ^
6422292
0061FF14

I am expecting an error in storing of address in integer variable.

dbush
  • 205,898
  • 23
  • 218
  • 273
Lokesh
  • 21
  • 1
  • 3
    C is traditionally forgiving on things like this. Deep down inside, a pointer typically *is* an integer, and some C programmers (especially old-school ones, with an assembly-language background), like to assume this. So in many C compilers, it's a warning, not an error. But these days, a warning is almost as good (bad) as an error; you generally don't want to ignore warnings. – Steve Summit Jan 03 '23 at 14:01
  • Learn [what and how to ask here](https://stackoverflow.com/tour) – ryyker Jan 03 '23 at 14:10
  • 1
    `I am expecting an error in storing of address in integer variable.` why? It simply converts the pointer to an integer. But you can make it an error by adding `-Werror` in the gc command line – 0___________ Jan 03 '23 at 14:15
  • "I am expecting an error ...." Well, then your expectations are wrong. **But**.... Always compile with `-Werror` (gcc option) and you will get your error – Support Ukraine Jan 03 '23 at 14:22
  • @SteveSummit "C is traditionally forgiving on things like this" No it isn't, this has been a constraint violation since day one of early C89 drafts. It is the gcc compiler specifically, which is being lax and generating an executable even though the code contains constraint violations. Then other gcc-like compilers decided to mimic it. – Lundin Jan 03 '23 at 14:56

2 Answers2

2

how is this possible?? … I am expecting an error in storing of address in integer variable.

int y=&a; violates the constraints for simple assignment in C 2018 6.5.16.1 1, which list several combinations of types allowed for the right operands. The only combination in which the left operand has arithmetic type other than _Bool, as int does, says the right operand must also have arithmetic type, which &a does not:

… the left operand has atomic, qualified, or unqualified arithmetic type, and the right has arithmetic type…

C 2018 5.1.13 1 says what a conforming C implementation must do for this:

A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint,…

A warning is a diagnostic message. Therefore it satisfies the requirements of the C standard. This answers your question “how is this possible?”

Enable warnings in your compiler and elevate warnings to errors. With Clang, start with -Wmost -Werror. With GCC, start with -Wall -Werror. With MSVC, start with /W3 /WX.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
2

  • without an error(just a warning)

    There is no such thing as "just a warning". A warning typically means: "here is a severe bug which you must fix to make your program behave as intended". It does not typically mean: "here is some minor cosmetic issue which you can ignore".



Lundin
  • 195,001
  • 40
  • 254
  • 396