-2

I'm learning pointers in C. I'm having confusion in Pointer arithmetic. Have a look at below program :

#include<stdio.h>
int main() 
 {
  int a[] = 2,3,4,5,6;
  int *i=a;
  printf("value of i = %d\n", i); ( *just for the sake of simplicity I have use %d* ) 
  printf("value of i+2 = %d\n", i+2); 
  return 0;
  } 

My question is if value of i is 653000 then why the value of i+2 is 653008 As far as I know every bit in memory has its address specified then according to this value of i+2 should be 653064 because 1 byte = 8 bit. Why pointer arithmetic is scaled with byte why not with bit? THANKS in advance and sorry for my bad English!

  • i+1 points to the next element. The compiler knows that i points to an integer, which is 4 bytes in size. In order to get the address of the next byte, cast i to a byte* – alzaimar Feb 19 '15 at 06:21
  • 2
    `"just for the sake of simplicity I have use %d"`. **No.** Pointers are shown in hex, with the `"%p"` format specifier. – Jonathon Reinhart Feb 19 '15 at 06:21
  • @JonathonReinhart: I think, for the *sake of simplicity*, one can use %d – alzaimar Feb 19 '15 at 06:23
  • 1
    @alzaimar No. There is no added simplicity. If one cannot understand hexadecimal notation, then one should not be using pointers. Not to mention, if compiler warnings are enabled and heeded (e.g. `gcc -Wall`), you would immediately realize that you're passing a *pointer* when `printf` is expecting an *integer*, which may be a different size. – Jonathon Reinhart Feb 19 '15 at 06:23
  • To my understanding, using %p does not answer his question. I prefer reducing answers to the questions posted. He did not seek advice in how to improve the display format of a pointer. – alzaimar Feb 19 '15 at 06:50
  • 1
    @alzaimar Which is why I mentioned it in a *comment*, not an answer. You're distracting from practical advice. – Jonathon Reinhart Feb 19 '15 at 06:52

3 Answers3

4

As far as I know every bit in memory has its address specified

Wrong.

Why pointer arithmetic is scaled with byte why not with bit?

The byte is the minimal addressable unit of storage on a computer, not the bit. Addresses refer to bytes - you cannot create a pointer that points to a specific bit in memory1.

Addresses refer to *bytes*
  |
  |
  v     _______________
0x1000 |_|_|_|_|_|_|_|_|    \
0x1001 |_|_|_|_|_|_|_|_|     > Each row is one byte
0x1002 |_|_|_|_|_|_|_|_|    /

       \_______ _______/
               v
       Each column is one bit

As others have explained, this is basic pointer arithmetic in action. When you add n to a pointer *p, you're adding n elements, not n bytes. You're effectively adding n * sizeof(*p) bytes to the pointer's address.

1 - without using architecture-specific tricks like Bit-banding on ARM, as myaut pointed out

Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Why? if byte is form with bits then according to this i+2 should be 653064 –  Feb 19 '15 at 06:31
  • No. Stop referring to bits, they're completely irrelevant here. Pointers and addresses refer to ***bytes*** only. – Jonathon Reinhart Feb 19 '15 at 06:32
  • > you cannot create a pointer that points to a specific bit in memory. Challenge accepted! http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0337h/Behcjiic.html – myaut Feb 19 '15 at 06:41
  • 1
    @myaut Awesome. I've updated my answer to add this exception. Thanks for pointing out such a cool feature. – Jonathon Reinhart Feb 19 '15 at 06:44
-1

You should read about the pointer arithmetic.link given in the comment.

While incrementing the position of pointer , that will incremented based on the data type of that pointer.In this case i+2 will increment the byte into eight bytes.

Integer is four bytes.(system defined). So i+2 will act as i+(2*sizeof(int)). So it will became i+8. So the answer is incremented by eight.

Addresses are calculating by the byte. Not the bit. Take a character pointer. Each byte having the 255 bits.

Consider the string like this. `"hi". It will stored like this.

  h     i
  1001  1002 

Ascii value of h is 104. It will be stored in one byte. signed character we can store positive in 0 to 127. So storing the one value we need the one byte in character dataype. Using the bits we cannot store the only value. so the pointer arithmetic is based on bytes.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
-2

When you do PTR + n then simple maths will be like

PTR + Sizeof(PTR)*n.

Here size of integer pointer is 4 Byte.

Elixir Techne
  • 1,848
  • 15
  • 20