-2

I wrote the following Code.

#include<stdio.h>

int main()
{
    int x = 1 ;
    int *j = &x ;
    int y =  2 ;
    int *t = &y ;

    printf("%p\n" , (void *)j);
    printf("%p" , (void *)t);
}   

Output is 0028FF14 0028FF10.

The Point I want to make is that the difference between the addresses is `4'.

Whereas in this case

#include<stdio.h>

int main()
{
    char x = 't' ;
    char *j = &x ;
    char y =  'f' ;
    char *t = &y ;
    printf("%p\n" , (void *)j);
    printf("%p" , (void *)t);    

   }   

Output is 0028FF17 0028FF16

The difference is 1.

Difference In First Case is 4. Whereas in the second case it is 1. Why is it so?

And What will I get if I printed value at all memory addresses individually?

Maybe It is really general and known, but I just started C, So the output of the program confuses me.

Update
Now Using %p format and converted the pointer value to void* to print the pointer value as suggested by Keith Thompson.

  • 1
    @SergeyK.: No, I don't think it's a duplicate. No pointer is being incremented. The code is just examining the addresses of declared objects. – Keith Thompson Jan 13 '16 at 18:36
  • @KeithThompson However, the underlying machinery is the same and the explanation is fully applicable to this case. – Sergey K. Jan 13 '16 at 18:41
  • 3
    @SergeyK.: I disagree. For `pointer ++`, the relationship is well defined. There is no defined relationship between the addresses of two separately defined objects. The addresses of `char x;` and `char y;` could easily differ by 4, for example -- or by 42. – Keith Thompson Jan 13 '16 at 18:44
  • 1
    @SergeyK.: How is this a duplicate? There is no use of `++` or any other pointer arithmetic in this question. – Keith Thompson Jan 13 '16 at 18:53
  • @KeithThompson: the question already has an answer there http://stackoverflow.com/q/5610298/1065190 – Sergey K. Jan 13 '16 at 20:20
  • 3
    @SergeyK.: You already said that. I'm saying this question is not a duplicate of that one. The other question asks about `++` on pointers. This one does not; it's not about pointer arithmetic at all. (There might be other reasons to close this question, but not as a duplicate of a question about `++`.) – Keith Thompson Jan 13 '16 at 20:36
  • @KeithThompson The closing reason is explicit and says "This question already has an answer here". It does not say anything if the question is duplicate or not. – Sergey K. Jan 13 '16 at 21:37
  • @SergeyK.: Which answer to the linked question is an answer to *this* question? (IMHO, none of them.) – Keith Thompson Jan 13 '16 at 22:06
  • 1
    Hmm? Are you saying you were asking the same question as the one about `++` on pointers? I really don't think you were. – Keith Thompson Jan 13 '16 at 22:15
  • 1
    A pointer contains a single address. Whether it's the address of a `char` or of an `int` depends on the type of the address. In C, an "address" is a value of pointer type. And addresses are not integers. On the other hand, each of the 4 (for example) bytes making up an `int` object is itself an object with its own unique address. – Keith Thompson Jan 13 '16 at 23:10
  • 1
    Again: **In C**, an "address" is a value of pointer type. The word "address" is used in other ways in other contexts. – Keith Thompson Jan 14 '16 at 00:18
  • 1
    After `foo` ends, the value of `g_ptr` becomes indeterminate as the lifetime of `x` is over: `g_ptr` has value `&x` which is now an invalid address. So use of `g_ptr` value produces undefined behavior after `foo` ends. – ouah Aug 17 '16 at 09:06
  • 1
    @SurajJain; I rollbacked to original question. Your edit made all the answers on this post invalid. If you have any other question then please ask in another post. – haccks Aug 17 '16 at 09:09
  • @SergeyK. Please Check The Question Again –  Aug 17 '16 at 09:21
  • @SurajJain as haccks mentioned, do not change a question that has answers, ask a new question. – ouah Aug 17 '16 at 16:01
  • @SurajJain I'm not the downvoters and I didn't follow this question, so I can just guess: one of these: duplicate, lack of research, bad quality question or total change of question after answers. Ask the downvoters. – ouah Aug 17 '16 at 18:21
  • @SergeyK. How is the question duplicate ? –  Jan 31 '17 at 12:01
  • I agree with @KeithThompson. – Kcvin Feb 02 '17 at 16:25

3 Answers3

3

There are no requirements on the order in which declared objects are laid out in memory. Apparently the compiler you're using happens to place x and y next to each other. It could have placed j between them, but it didn't.

Also, the correct way to print a pointer value is to use the %p format and convert the pointer value to void*:

printf("%p\n", (void*)j);
printf("%p\n", (void*)t);

This produces an implementation-defined human-readable representation of the pointer value, typically but not always in hexadecimal.

If you care about the order in which declared variables are allocated in memory, you're probably doing something wrong, or at least not useful. Let the compiler worry about where to put things. It knows what it's doing.

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
  • I'm not sure what you mean by that. A `char` object is allocated exactly 1 byte. An `int` object is allocated `sizeof (int)` bytes. Objects can be allocated in any order, and there may or may not be extra padding between them. – Keith Thompson Jan 13 '16 at 18:48
  • 1
    I don't understand what you mean. An address of type `int*` refers to the location of an `int` object. That object occupies `sizeof (int)` bytes, typically 4. But when you print the value of a pointer, it typically reflects the address of the first byte of the object it points to. Is there some particular problem you're trying to solve here? In my opinion your question is not a duplicate. – Keith Thompson Jan 13 '16 at 18:52
  • @SurajJain: Sure, a pointer to an N-byte type will point to an N-byte object. – Keith Thompson Jan 13 '16 at 18:54
  • 1
    @SurajJain: An `int*` contains the address of an `int` object. If you convert an `int*` value to `char*`, the result contains the address of the first byte of the `int` object. – Keith Thompson Jan 13 '16 at 18:58
  • 1
    The type of a pointer tells you what type of object it points to. An `int*` points to an `int` object. You might want to read section 4 of the [comp.lang.c FAQ](http://www.c-faq.com/). – Keith Thompson Jan 13 '16 at 20:11
  • @SurajJain: An pointer don't "contain one byte only", it holds a value, which is the address _of the start_ of an object. An object can only start at one address, regardless of it's size. If a pointer points at an int, then everyone knows it points at a the start of an int. If you increment it, it will then point at the subsequent int. All pointers point "at the start" of an object. That's what computer addresses are. – Mooing Duck Jan 14 '16 at 00:28
  • @SurajJain: No, a pointer holds the address of the start of an object. Nothing to do with 1 byte. An int takes several bytes, and the pointer points at the start of that block of bytes. (It's not always four bytes). In a BE system, the MSB will be in the first byte, in a LE system, the LSB will be in the first byte. – Mooing Duck Jan 14 '16 at 00:49
  • 1
    Ok, so that's the source of the confusion. You used C code in your question, and in C an "address" is a (typed) pointer value. In machine terms, an "address" typically designates a single byte. The address of a 4-byte object is the same as the address of the *first* of those 4 bytes. If that base address is, say, 1000, then addresses 1001, 1002, and 1003 will designate successive bytes within that 4-byte object. – Keith Thompson Jan 14 '16 at 01:33
  • @SurajJain: Ah, the compiler sets aside 4 _bytes_ of memory. If you print each of these bytes individually, you'll get the bytes that make up the integer. The order of these bytes is different on each architecture. Sometimes it even changes while the machine is running. But an object will always be saved using consecutive bytes. – Mooing Duck Jan 14 '16 at 21:40
  • @SurajJain: I didn't downvote your question. Don't ask me why others did. – Keith Thompson Jan 30 '17 at 07:45
2

Well for starters the difference isn't always four, it just happens to be four by happy coincidence. The compiler is allowed to stick variables in memory where ever it wants to. In this case it has put your two variables next to each other in memory and the difference can be explained as that is how big an integer is on your system(4 bytes) and how big a character is on your system(1 byte). On other systems they may be different sizes and placed in different locations.

stonemetal
  • 5,972
  • 21
  • 25
1

Each integer takes up four bytes, therefore, each integer memory address is offset by 4. A char only takes up one byte, therefore its memory addresses are offset by one.

Ira Rodens
  • 111
  • 3
  • What will happen if i printed value at addresses which lie in between them ? –  Jan 14 '16 at 00:25
  • You will get the individual bytes composing the number. For example if the int value is 1, then it would 0x00000001 in hex which would break down into the following char array 0x00,0x00,0x00,0x01. – Ira Rodens Jan 14 '16 at 02:01