-1

i got below code , i got same value from both variable but assignment b is throw a error and what is the value is it after print ,the value is 134513904 from both variables what is this

int a=(int)"aa",b="aa";
printf("%d,%d",a,b);

if.c: In function ‘main’:
if.c:6:19: warning: initialization makes integer from pointer without a cast [enabled by default]
 cast [enabled by default]
 int a=(int)"aa",b="aa";
                   ^
134513904,134513904
 shell returned 19

Then: printf("%s:%s",a,b); it print string =>aa:aa

Hema Krish
  • 19
  • 2
  • This warning should almost always be considered as an error. – Jabberwocky Nov 06 '19 at 06:55
  • can you please tell what is error and what is warning? how you say warning is consider to be error. – Hema Krish Nov 06 '19 at 07:03
  • 1
    Error: you did something completely wrong and the program cannot be compiled. Warning: you did something wrong but you might get away with it if you know exactly what you're doing. – Jabberwocky Nov 06 '19 at 07:07
  • Possible duplicate of [Addresses of two char pointers to different string literals are same](https://stackoverflow.com/questions/19088153/addresses-of-two-char-pointers-to-different-string-literals-are-same) – Raman Nov 06 '19 at 07:13
  • 'KP Creations' asked the same, an hour or so ago. – Martin James Nov 06 '19 at 07:30

1 Answers1

0

the value is 134513904 from both variables what is this

It is the address of location where string aa is stored. That is only one copy of aa is stored into 'ready-only' section and its address is stored in a and b variables.

Since int are not supposed to hold the address, compiler is throwing warning.

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44