-4
#include<stdio.h>
int main()
{
    int i = 5;
    float a = 3.14;
    char*ii,*aa;

    ii = (char*)&i;
    aa = (char*)&a;
    printf("address contained in ii=%u\n",ii);
    printf("address contained in aa=%u\n",aa);
    printf("value at address contained in ii=%d\n",*ii);
    printf("value at address contained in aa= %d\n",*aa);
    return(0);
} 
/*the output for the following program was 
  address contained in ii=65524

 address contained in aa=65520
  value at address contained in ii=5

  value at address contained in aa=-61*/

What is the meaning of ii = (char*) &i and aa = (char*) &a also why does the compiler print value at address in ii to be correct and that in aa to be wrong? If you take any other value of i, say 327, then the value at address contained in ii will turn out to be something else. Can someone please explain it to me? I am not able to get a proper explanation from the book where the code is written.

Program source: understanding pointers in c. Author : yashvant kanetkar,4th edition.

rookie
  • 5
  • 5
  • 1
    That program has issues, it's probably very old. To print the address of a pointer you need to cast to `void *` and use the `"%p"` `printf()` specifier. The program is also incorrect, the author is dereferencing the pointer through an aliased pointer which invokes undefined behavior. – Iharob Al Asimi Jun 30 '15 at 12:53
  • 1
    The program shouldn't compile. How did you get *output*? – P.P Jun 30 '15 at 12:53
  • @BlueMoon I didn't see the `float a = 3,14`. – Iharob Al Asimi Jun 30 '15 at 12:54
  • 1
    @iharob I didn't see that either :) Also there are multiple `i` and `a` variables. – P.P Jun 30 '15 at 12:56
  • 2
    @BlueMoon Oh ... I missed that too and also, none of the pointers is initialized, this program is all undefined behavior if it were to ever compile. I think the OP meant `char **ii, **aa;` and since the Ctrl+A Ctrl+C Ctrl+V is so difficult and takes so much time, they preferred to type the whole program again. – Iharob Al Asimi Jun 30 '15 at 12:57
  • 2
    Ditch the Kanetkar book - seriously - all his books are terrible - see [this question](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) for a list of much better books to learn C from. – Paul R Jun 30 '15 at 12:58
  • 3
    @PaulR There are a lot of terrible books, I think it's because good programmers do programming and teachers are so because they were not as good writing programs. Except for very few cases like Knuth and Stroustrup. – Iharob Al Asimi Jun 30 '15 at 13:00
  • Yes, it's rare to find someone who is both a good programmer and a good teacher/writer, but there are a few out there. Kanetkar though is one of the worst examples - his books are full of misinformation, bad code, undefined behaviour, etc, and they are seriously out-of-date. Unfortunately his books seem to be standard in Indian colleges, so every year we get a new crop of novice programmers who badly need to unlearn a lot of what they think they know about C/C++. – Paul R Jun 30 '15 at 13:03
  • @rookie: you've edited the code now but it still doesn't even compile - please only post your *actual code* (hint: use copy and paste). – Paul R Jun 30 '15 at 13:06
  • 1
    Sorry, but this is really from a published book? – lord.garbage Jun 30 '15 at 13:15
  • I also invite @haccks sir to read the question. [Note: see the book name] :-) – Sourav Ghosh Jun 30 '15 at 13:15
  • @brauner sadly, yes.... – Sourav Ghosh Jun 30 '15 at 13:15
  • 1
    Why is the first pointer printed with `%u` and the second with `%d`? Both are wrong, but in any case: why the difference? – AnT stands with Russia Jun 30 '15 at 13:23
  • I see that the OP has now edited the program to be very much closer to the text from the source document. – quamrana Jun 30 '15 at 13:48

1 Answers1

1

The program as given in the book is an example of what not to do:

Don't try to access a value through a pointer when the pointer is not a pointer to the same type as the original value.

In the book, i is an int, but the code deliberately makes a char* point to the integer. So, when ii is dereferenced you just get a value that seems to be a char.

edit: So, the expression (char*)&i; first takes the address of i, which the compiler thinks of as type int* and then converts the type to char* without changing the value of the pointer.

The important thing to note about the different types of pointers is that they often imply a different size of object pointed to.

Also note that the output of all the whole program will depend on the platform you are using.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • agreed..... but what does the statement (char*)&i do ? does it make ii think that the address stored in it is that of a character ?? – rookie Jul 01 '15 at 11:57