1

Lets take 64 bit machine

where pointer is of 8 bytes in 64 bit machine

int *p ; // it is a pointer to integer variable so when i increment p
         // i.e., p++ it will increment by 4 

char *r; // It is pointer to character . 
         // So if i increment 'r' it will increment by 1

int **q ; // if i increment q  ie.,q++ it will increment 8 bytes 

i tried this peace of code if any thing wrong please correct me

int a=10; 
int *p; 
char *r; 
int **q; 
p=&a; 
q=&p; 
printf("p= %p\t r= %p\t q=%p\n",p,r,q);
 printf("p(increment)= %p\t r (increment)= %p\tq (increment)= %p ",++p,++r,++q); 

output

 p= 0x7fff669bb1bc r= 0x7fff669bb2a0 q=0x7fff669bb1a0
 p(increment)= 0x7fff669bb1c0 r (increment)= 0x7fff669bb2a1 q (increment)= 0x7fff669bb1a8

what is role of int/char/float in double pointer?

Vivek
  • 181
  • 3
  • 10
  • sorry this is in new line int **q ; // if i increment q ie.,q++ it will increment 8 bytes what is role of int/char/float in double pointer? Thanks in advance – Vivek Feb 01 '16 at 07:08
  • 2
    `where pointer is of 8 bytes in 64 bit machine` are you sure, did you try the MSVC? – Sourav Ghosh Feb 01 '16 at 07:09
  • in linux 64 bit machine pointer is of size 8 – Vivek Feb 01 '16 at 07:12
  • 1
    On that machine. There is no size guarantee for pointers in general. – StoryTeller - Unslander Monica Feb 01 '16 at 07:13
  • @Vivek Is it? can you show me something (quote) that guarantees that? – Sourav Ghosh Feb 01 '16 at 07:18
  • i tried this peace of code if any thing wrong please correct me int a=10; int *p; char *r; int **q; p=&a; q=&p; printf("p= %p\t r= %p\t q=%p\n",p,r,q); printf("p(increment)= %p\t r (increment)= %p\tq (increment)= %p ",++p,++r,++q); output p= 0x7fff669bb1bc r= 0x7fff669bb2a0 q=0x7fff669bb1a0 p(increment)= 0x7fff669bb1c0 r (increment)= 0x7fff669bb2a1 q (increment)= 0x7fff669bb1a8 – Vivek Feb 01 '16 at 07:30
  • @Vivek Use the edit button of the question to add additional details. – Lundin Feb 01 '16 at 07:50

3 Answers3

5

To quote the property of the postfix ++ operator, from C11, chapter §6.5.2.4, Postfix increment and decrement operators, (emphasis mine)

The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it). [...]

  • In case of int *p ;, p is a pointer to type int, hence the increment will be based on sizeof(int)

  • In case of int **p ;, p is a pointer to type int *, hence the increment will be based on sizeof(int *)

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • This cited text doesn't say much. The very next sentence is what's of interest: `See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers.` The explanation of how pointer arithmetic works is found at 6.5.6/7 and beyond. – Lundin Feb 01 '16 at 07:31
  • @Lundin Yes sir, very true. I just wanted to point out the _appropriate type_ part. I felt extending the discussion to _how_ will be a little extra in this context. Do you suggest I extend this answer to cover that aspect? – Sourav Ghosh Feb 01 '16 at 07:36
  • I don't think any of the standard text is particularly helpful here (particularly not the mildly readable 6.5.6), as we are dealing with beginners. – Lundin Feb 01 '16 at 07:41
0

Well, this depend not only on your machine, but also on your implementation. You can figure them out by outputting sizeof p, sizeof r, and sizeof q. Printing sizeof (int *), sizeof (char *), and sizeof (int **) works too.

C programmers traditionally like to know a lot (perhaps more than they need to) about the underlying machine implementation.

From my perspective, you don't need to know these stuffs, because the compiler will handle this for us (according to the C standard) quite well. Also, most practices making use of these stuffs are essentially undefined behaviors.

nalzok
  • 14,965
  • 21
  • 72
  • 139
-2

In int **p you increment the pointer by the size it points to (i.e. pointer). As the size of pointer is 8, it will increment by 8.

The final type has no role here. It will behave the same like the pointer to pointer to void.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
  • Pointers aren't guaranteed to have the same size. C permits: sizeof( void* ) != sizeof( int* ) – 2501 Feb 01 '16 at 07:14
  • @2501 That's incorrect, show me the specification. The pointers are always the same size and you can easily cast from one to another. The only exception is some old DOS models with far and near pointers but still they don't differ per type. – Zbynek Vyskovsky - kvr000 Feb 01 '16 at 07:17
  • 2
    The permission is by omission. There is no requirement in that standard that all pointers should be of the same size. – StoryTeller - Unslander Monica Feb 01 '16 at 07:19
  • 2
    @ZbynekVyskovsky-kvr000 The c standard does not explicitly mandate that all pointers be the same size. – Magisch Feb 01 '16 at 07:19
  • 1
    The C standard does however mandate that all pointers should be convertible to `void*` and then back again. If "any pointer type" had a different size than `void*` that wouldn't be possible. Thus every pointer type has to be of the same size. In practice, all CPUs that have extended addresses uses some non-standard syntax for that. – Lundin Feb 01 '16 at 07:25
  • 1
    @Lundin You're assuming that a larger type cannot hold a smaller type. I don't think that is correct. Why couldn't a void* hold a smaller type? – 2501 Feb 01 '16 at 07:26
  • @Lundin Why are we even arguing, this is an answered problem: https://stackoverflow.com/questions/4192578/are-pointers-of-the-same-size (Click on the duplicates, they provide better answers.) – 2501 Feb 01 '16 at 07:27
  • @Lundin, given the technology that exists today, you are empirically correct. But the wording in the standard is what it is to be future proof. The empirical assumption about pointer sizes that are identical may be incorrect on future hardware, and under a `c` standard conforming compiler no less. – StoryTeller - Unslander Monica Feb 01 '16 at 07:32
  • I've worked with a couple of exotic architectures and all of them completely ignored the C standard for their peculiar pointer types, and invented a non-standard syntax of their own (near & far keywords etc). There's a reason for that: you want the normal addressing to behave as on any computer, and for that you follow the C standard. And then you use non-standard code for the extended addressing. The C standard as it currently stands is too blunt to support such systems. It isn't helpful to pretend otherwise. – Lundin Feb 01 '16 at 07:48
  • @Lundin, Again, just because those architectures opted for one thing doesn't mean compliance to the standard forces pointers to be of the same size. An esoteric architecture where `sizeof(int*) != sizeof(void*)` is not impossible. – StoryTeller - Unslander Monica Feb 01 '16 at 07:53
  • @StoryTeller That's what I said: the standard is too blunt to be useful in this case, so what it says doesn't really matter, until they fix it (by for example adding the `near` and `far` keywords). If you are worried about your standard pointers having different sizes, for theoretical/mysterious/magical reasons, and you have code relying on a pointer type's size, then simply add this `_Static_assert(sizeof(type*) == sizeof(void*), "Error: magic pointer found.");`. – Lundin Feb 01 '16 at 08:02
  • @Lundin, I misunderstood you. I'm not worried about it. I just push all code that assumes such things into neat little translation units that are `#ifdef`-ed out (and add an assert in-case my build system hiccups). – StoryTeller - Unslander Monica Feb 01 '16 at 08:04