36
#include<stdio.h>
int main(void){
  int *ptr,a,b;
  a = ptr;
  b = ptr + 1;
  printf("the vale of a,b is %x and %x respectively",a,b);

  int c,d;
  c = 0xff;
  d = c + 1;
  printf("the value of c d are %x and %x respectively",c,d);
  return 0;
}

the out put value is

the vale of a,b is 57550c90 and 57550c94 respectively
the value of c d are ff and 100 respectively%  

it turns out the ptr + 1 actually, why it behave this way?

4
  • 1
    See my previous answer to a question like this. Jul 22, 2012 at 7:14
  • You should use %p in printf format strings for pointers. Don't use %x for that, it is not portable (because pointers and int-s may have different bit width). Jul 22, 2012 at 8:12
  • You really should enable all warnings for your compiler, e.g. -Wall if compiling with gcc; your code has several mistakes! In particular ptr remains uninitialized but is used (undefined behavior). Jul 22, 2012 at 8:20
  • @BasileStarynkevitch Yes you are right. I just check the %xand '%p' , there's difference.And for clang compiler, does it also can be use -Wall to enable warning?
    – mko
    Jul 22, 2012 at 8:28

6 Answers 6

52

Because pointers are designed to be compatible with arrays:

*(pointer + offset)

is equivalent to

pointer[offset]

So pointer aritmetic doesn't work in terms of bytes, but in terms of sizeof(pointer base type)-bytes sized blocks.

7
  • for 64 bit machines, it may be as big as 8 bytes.
    – Aftnix
    Jul 22, 2012 at 7:10
  • @Aftnix yes, but what's the problem with that?
    – user529758
    Jul 22, 2012 at 7:11
  • a pointer will be big enough to hold a memory address of the platform. If you need 8 bytes to hold memory address, it will be 8bytes regardless of what its pointing to. I'm just saying that you should point this out in your answer for the sake of generality. there is nothing wrong with your answer, but pointer arithmetic makes sense if you have "fixed" sized pointer variable. I'm just saying before stating the principle of pointer arithmetic, you should say that every platform has a native fixed pointer size. Thats why pointer arithmetic is consistent in different platform.
    – Aftnix
    Jul 22, 2012 at 7:20
  • +1 for letting me know the connection between array and pointer. but I can't make your code work: printf("%p",ptr[1])
    – mko
    Jul 22, 2012 at 8:22
  • euh not, printf("%p", ptr + 1). Note that *(ptr + offset) is equivalent to ptr[offset], but ptr + offset itself is not!
    – user529758
    Jul 22, 2012 at 10:04
42

Consider what a pointer is... it's a memory address. Every byte in memory has an address. So, if you have an int that's 4 bytes and its address is 1000, 1001 is actually the 2nd byte of that int and 1002 is the third byte and 1003 is the fourth. Since the size of an int might vary from compiler to compiler, it is imperative that when you increment your pointer you don't get the address of some middle point in the int. So, the job of figuring out how many bytes to skip, based on your data type, is handled for you and you can just use whatever value you get and not worry about it.

As Basile Starynkvitch points out, this amount will vary depending on the sizeof property of the data member pointed to. It's very easy to forget that even though addresses are sequential, the pointers of your objects need to take into account the actual memory space required to house those objects.

0
11

Pointer arithmetic is a tricky subject. A pointer addition means passing to some next pointed element. So the address is incremented by the sizeof the pointed element.

0
5

Short answer

The address of the pointer will be incremented by sizeof(T) where T is the type pointed to. So for an int, the pointer will be incremented by sizeof(int).

Why?

Well first and foremost, the standard requires it. The reason this behaviour is useful (other than for compatibility with C) is because when you have a data structure which uses contiguous memory, like an array or an std::vector, you can move to the next item in the array by simply adding one to the pointer. If you want to move to the nth item in the container, you just add n.

Being able to write firstAddress + 2 is far simpler than firstAddress + (sizeof(T) * 2), and helps prevent bugs arising from developers assuming sizeof(int) is 4 (it might not be) and writing code like firstAddress + (4 * 2).

In fact, when you say myArray[4], you're saying myArray + 4. This is the reason that arrays indices start at 0; you just add 0 to get the first element (i.e. myArray points to the first element of the array) and n to get the nth.

What if I want to move one byte at a time?

sizeof(char) is guaranteed to be one byte in size, so you can use a char* if you really want to move one byte at a time.

3
  • std::vector is not C. Dec 21, 2020 at 14:07
  • 1
    Std::Vector is only in C++ which has its differences to C. Dec 23, 2020 at 20:17
  • @newQOpenGLWidget it's true that C and C++ have differences, but pointer addition is not one of them. Feb 5 at 19:24
2

A pointer is used to point to a specific byte of memory marking where an object has been allocated (technically it can point anywhere, but that's how it's used). When you do pointer arithmetic, it operates based on the size of the objects pointed to. In your case, it's a pointer to integers, which have a size of 4 bytes each.

1

Let consider a pointer p. The expression p+n is like (unsigned char *)p + n * sizeof *p (because sizeof(unsigned char) == 1). Try this :

#include <stdio.h>
#define N   3

int
main(void)
{
    int i;
    int *p = &i;
    printf("%p\n", (void *)p);
    printf("%p\n", (void *)(p + N));
    printf("%p\n", (void *)((unsigned char *)p + N * sizeof *p));
    return 0;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.