Pointer Arithmetic
V. Kamakoti
Rule - 1
• A pointer variable can be assigned the
address of an ordinary variable.
• int v, *pv;
– pv = &v;
– char c;
– pv = &c; //shall give a warning incompatible types
Rule - 1
• Casting is possible
• unsigned *pv;
– char c[5]; c[0] = ‘0’;c[1]=‘1’;c[2]=‘2’;c[3]=‘3’;
– pv = (unsigned *) c;
– printf(“%d %d %d %d %un”,
c[3],c[2],c[1],c[0],*pv);
– In x86 m/c Shall print - 51,50,48,49,858927408
– Little Endian storage of Integers
• Least significant byte in the least address - x86
representation
• Big Endian - Most significant byte in least address - Sun
Sparc
• In Sparc m/c print out shall be 51,50,48,49, 808530483
Rule 1
•
•
•
•

Character casting gives error in gcc
unsigned *pv; char *c;
c = (char *) pv; //Gives error
Reason: Not every ASCII character is
usable inside C
Rule - 2
• A pointer variable can be assigned the
value of another pointer variable
provided they are of same type
– int *pw, *pv;
– pv = pw;
– float *pk;
– pv = pk; //Gives a warning
Rule - 3
• A pointer variable can be assigned a
null value
– int *pw;
– pw = NULL;

• NULL is a symbolic constant;
Rule - 4
• An integer quantity can be added to or
subtracted from a pointer variable
– int *pw;
– pw++; pw+3;
– The amount of increment depends on the
type of the pointer variable
Rule - 5
• One pointer variable can be subtracted from
another provided. It makes sense only if both
points to elements in the same array
–
–
–
–
–

int *pw, *pv, A[100];
pv = &A[51];
pw = &A[75];
printf(“%d”,pw - pv); Ans: 24
printf(“%d”,pv - pw); Ans: -24
Rule - 5
• One pointer variable can be subtracted from
another provided. It makes sense only if both
points to elements in the same array
–
–
–
–
–

int *pw, *pv, A[100], B[100];
pv = &A[51];
pw = &B[75];
printf(“%d”,pw - pv); Ans: Not clear
printf(“%d”,pv - pw); Ans: Not clear
Rule - 6
• Two pointer variables can be compared if
they point to objects of the same type
– int *pw, *pv;
– pw < pv; pw >= pv; pw <= pv; pw == pv; pw != pv;
pw == NULL
– char c[4]; if (pw < c) … ; gives a Warning
– pw < (int *) c - passes
– (char *) pw < c - also passes
Do Not Rules
• Pointer variable cannot be multiplied by a constant
• Pointer variables cannot be added
• Ordinary variables cannot be assigned an arbitrary
address.
• int x;
• &x = … //Gives error
• int *pv,*pw;
• pv + pw or 2*pw …. // Invalid operands for +, * etc.
• pv = pv - pw // Wrong - as pv - pw is an integer and
not a integer pointer
• Pv = (int *) (pv - pw) passes compilation.

Lec25-CS110 Computational Engineering

  • 1.
  • 2.
    Rule - 1 •A pointer variable can be assigned the address of an ordinary variable. • int v, *pv; – pv = &v; – char c; – pv = &c; //shall give a warning incompatible types
  • 3.
    Rule - 1 •Casting is possible • unsigned *pv; – char c[5]; c[0] = ‘0’;c[1]=‘1’;c[2]=‘2’;c[3]=‘3’; – pv = (unsigned *) c; – printf(“%d %d %d %d %un”, c[3],c[2],c[1],c[0],*pv); – In x86 m/c Shall print - 51,50,48,49,858927408 – Little Endian storage of Integers • Least significant byte in the least address - x86 representation • Big Endian - Most significant byte in least address - Sun Sparc • In Sparc m/c print out shall be 51,50,48,49, 808530483
  • 4.
    Rule 1 • • • • Character castinggives error in gcc unsigned *pv; char *c; c = (char *) pv; //Gives error Reason: Not every ASCII character is usable inside C
  • 5.
    Rule - 2 •A pointer variable can be assigned the value of another pointer variable provided they are of same type – int *pw, *pv; – pv = pw; – float *pk; – pv = pk; //Gives a warning
  • 6.
    Rule - 3 •A pointer variable can be assigned a null value – int *pw; – pw = NULL; • NULL is a symbolic constant;
  • 7.
    Rule - 4 •An integer quantity can be added to or subtracted from a pointer variable – int *pw; – pw++; pw+3; – The amount of increment depends on the type of the pointer variable
  • 8.
    Rule - 5 •One pointer variable can be subtracted from another provided. It makes sense only if both points to elements in the same array – – – – – int *pw, *pv, A[100]; pv = &A[51]; pw = &A[75]; printf(“%d”,pw - pv); Ans: 24 printf(“%d”,pv - pw); Ans: -24
  • 9.
    Rule - 5 •One pointer variable can be subtracted from another provided. It makes sense only if both points to elements in the same array – – – – – int *pw, *pv, A[100], B[100]; pv = &A[51]; pw = &B[75]; printf(“%d”,pw - pv); Ans: Not clear printf(“%d”,pv - pw); Ans: Not clear
  • 10.
    Rule - 6 •Two pointer variables can be compared if they point to objects of the same type – int *pw, *pv; – pw < pv; pw >= pv; pw <= pv; pw == pv; pw != pv; pw == NULL – char c[4]; if (pw < c) … ; gives a Warning – pw < (int *) c - passes – (char *) pw < c - also passes
  • 11.
    Do Not Rules •Pointer variable cannot be multiplied by a constant • Pointer variables cannot be added • Ordinary variables cannot be assigned an arbitrary address. • int x; • &x = … //Gives error • int *pv,*pw; • pv + pw or 2*pw …. // Invalid operands for +, * etc. • pv = pv - pw // Wrong - as pv - pw is an integer and not a integer pointer • Pv = (int *) (pv - pw) passes compilation.