-1

Here is a basic C program about pointers:

#include <stdio.h>

int main() {
    int variable = 20;
    int *pointerToVariable;

    pointerToVariable = &variable;

    printf("Address of variable: %x\n", &variable);
    printf("Address of variable: %x\n", pointerToVariable);
    printf("Address of variable: %x\n", *pointerToVariable); // * is the DEREFERENCING OPERATOR/INDIRECTION OPERATOR in C. 
                                                                //It gives the value stored at the memory address 
                                                                //stored in the variable which is its operand.
    getchar();
    return 0;
}

This produces the following output:

Address of variable: 8ffbe4
Address of variable: 8ffbe4
Address of variable: 14

But *pointerToVariable should print 20, shouldn't it? Because * gives the actual value stored at the memory address stored in its operand, right? What am I missing?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Shy
  • 542
  • 8
  • 20
  • 2
    It does. `14` is `20` in hexadecimal – StoryTeller - Unslander Monica Mar 30 '17 at 06:16
  • 2
    `14` is the `HEX` value of `20`.....Change `printf` format specifier to `%d` instead of `%x`. – LPs Mar 30 '17 at 06:16
  • 2
    Use %d to see expected result. As previous comments explain, %x is printing hexadecimal – aicastell Mar 30 '17 at 06:18
  • OK But why did I get a downvote. All beginners make such mistakes, don't they? – Shy Mar 30 '17 at 06:31
  • 1
    @Sie it's not about the problem in the question, it is because the lack of research attempt shown by you. Please always show your research/debugging effort so far. Please read [Ask] page first. – Sourav Ghosh Mar 30 '17 at 06:32
  • 2
    It illegal (undefined behavior) to use `%x` to print `&variable` and `pointerToVariable`. How did you come up with that idea? If you use `printf` format specifiers frivolously and without care, you will see a lot of "strange" results in the output. Spamming SO with questions about such output will usually get you downvotes. And if you made the effort to read the docs to find the proper format specifier, you'd probaly figure out by yourself why the last one prints `14`. – AnT stands with Russia Mar 30 '17 at 06:35
  • @SouravGhosh Thank you. I will be careful in future. – Shy Mar 30 '17 at 06:49
  • @AnT I am following [this website](https://www.tutorialspoint.com/cprogramming/c_pointers.htm) to learn. I can only read one thing at a time and I am allowed to make mistakes because I am human. – Shy Mar 30 '17 at 06:57
  • 1
    @Sie: Well, the code samples at the link are of very bad quality. And the text is far from perfect too (to put it mildly). (After reading a few pages) Sorry, this tutorial is just bad, will do some damage and will require a lot of re-learning afterwards. – AnT stands with Russia Mar 30 '17 at 07:28
  • @AnT Thank you very much indeed. I am going to look for another tutorial. Most probably I am going for cprogramming.com – Shy Mar 30 '17 at 08:30

3 Answers3

1

First of all,

printf("Address of variable: %x\n", &variable);
printf("Address of variable: %x\n", pointerToVariable);

are wrong, because you used wrong format specifier which causes undefined behavior.

To print an address, you need to

  • use %p format specifier
  • cast the corresponding argument to (void *)

Then, for

printf("Address of variable: %x\n", *pointerToVariable);

statement, the %x format specifier prints the hexadecimal representation of the supplied integer value, so you've got the correct output there.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

14 is the HEX value of 20.

Change printf format specifier to %d instead of %x to have 20 as output

printf("Address of variable: %d\n", *pointerToVariable);

Moreover correct format specifier for pointers is %p, so

printf("Address of variable: %x\n", pointerToVariable);

must be

printf("Address of variable: %p\n", (void *)pointerToVariable);
LPs
  • 16,045
  • 8
  • 30
  • 61
1

Your formatting is hex (base 16) (%x) this line :

printf("Address of variable: %x\n", *pointerToVariable);// output: 14

If you want the output in base 10, then you need to provide the right formatting :

 printf("Address of variable: %d\n", *pointerToVariable); // output : 20

// 1*16 + 4 = 20

Good luck

kourouma_coder
  • 1,078
  • 2
  • 13
  • 24