-4

What is the difference between these two code samples? When I print the variable p, it prints the assigned value like below.

int *p;
p = 51;
printf("%d",p);

Output: 51

When I try to assign p=15, am I making memory address "15" in the ram as a pointee to the pointer p? When I try to add int c = 5 +p; it gives output as 71. Why am I getting 71?

I thought that the memory address "15" could store any information of the OS, programs, etc. But it exactly stores int for precise. Though I change the value p = 150; it gives int . How is that possible? What's happening under the hood?! I really don't understand.

Dagrooms
  • 1,507
  • 2
  • 16
  • 42
SkrewEverything
  • 2,393
  • 1
  • 19
  • 50
  • 1
    Actually, `int c = 5 +p` is [undefined behavior](http://stackoverflow.com/questions/30535699/will-this-result-in-a-seg-fault) – Eugene Sh. Jun 09 '15 at 21:58
  • Printing a pointer with `%d` is wrong. A pointer is 'something else' than an `int`. I get *two* (serious) warnings for these 3 lines of code. – Jongware Jun 09 '15 at 22:00

3 Answers3

5

Your code is illegal. Formally, it is not C. C language prohibits assigning integral values to pointer types without an explicit cast (with the exception of constant 0)

You can do

p = (int *) 51;

(with implementation-defined effects), but you cannot do

p = 51;

If your compiler allows the latter variant, it is a compiler-specific extension that has nothing to do with standard C language.

Typically, such assignment makes p to point to address 51 in memory.

On top of that, it is illegal to print pointer values with %d format specifier in printf. Either use %p or cast pointer value to proper integer type before using integer-specific format specifiers.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • But according to @ Rex Kerr I can do `p = 51;` and it works and it results in Undefined Behaviour. – SkrewEverything Jun 09 '15 at 22:30
  • @harish: Sorry, but you can't. One more time: if your compiler allows you to do that, it is a compiler extension that has nothing to do with standard C. And in any case, converting integer value to pointer type by itself results in implementation-defined behavior, not in undefined behavior. – AnT stands with Russia Jun 09 '15 at 22:31
  • So, if i want to know what's in the memory address `15` , can I print that by using `printf("%p",*p);` ? Just want to know out of curiosity. – SkrewEverything Jun 09 '15 at 22:42
  • @harish: Well, most of the time it won't work. Firstly, in modern OSes by doing this you are attemting to access address `15` in the virtual address space of your process. Secondly, if this address does not belong to some valid memory block owned by you, an attempt to read this memory location will trigger memory protection and crash your program. – AnT stands with Russia Jun 09 '15 at 22:47
0

So you're telling that pointer that it points to 0x15. Then, you tell printf to print it as a decimal integer, so it treats it as such.

This reason this works is that on a 32 bit system, a pointer is 4 bytes, which matches the size of an int.

cehnehdeh
  • 527
  • 3
  • 13
0

p points to a place in memory. *p is the contents of that space. But you never use the contents, only the pointer.

That pointer can be viewed as just a number, so printf("%d",p) works. When you assign a number to it, it interprets that as an offset into memory (in bytes). However, the pointer is supposed to contain ints, and when you add a number to a pointer, the pointer advances by that many spaces. So p+5 means "point to the int 5 spaces past the one you're pointing at now", which for 4-byte ints means 20 bytes later, hence the 71.

Otherwise, you've said you have a pointer to an int, but you're actually just doing all your stuff to the pointer, not the int it's pointing to.

If you actually put anything into the place you were pointing, you'd run into all kinds of trouble. You need to allocate some unused memory for it (e.g. with malloc), and then read and write values to that memory using *p.

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • So, can I print the value of 71th space by using `printf("%d",*p);` ? – SkrewEverything Jun 09 '15 at 22:03
  • @EugeneSh. - Yes, but the OP wanted to know why their code did what it did, which my answer explains. Just because it's undefined by some standard doesn't mean you cannot understand why it does what it does in your case. – Rex Kerr Jun 10 '15 at 02:38