Go to your University || Python Lab || DSA Lab || AI & Machine Learning Lab || Linux Lab || OOPs Lab || DBMS Lab || JAVA Lab ||| Free Online Tutorials ||| 
companies for Campus Placement || Logical Reasoning || Quantitative Aptitude || General English || Technical-MCQ and Interview Questions || HR Interview Questions

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.8k points)
 
Best answer

Pointer addition

Given a pointer and a scalar type N, evaluates into a pointer to the Nth element of the pointed-to type that directly succeeds the pointed-to object in memory.

int arr[] = {1, 2, 3, 4, 5}; 

printf("*(arr + 3) = %i\n", *(arr + 3)); /* Outputs "4", arr's fourth element. */

It does not matter if the pointer is used as the operand value or the scalar value. This means that things such as 3 + arr are valid. If arr[k] is the k+1 member of an array, then arr+k is a pointer to arr[k]. In other words, arr or arr+0 is a pointer to arr[0], arr+1 is a pointer to arr[2], and so on. In general, *(arr+k) is same as arr[k].

Unlike the usual arithmetic, addition of 1 to a pointer to an int will add 4 bytes to the current address value. As array names are constant pointers, + is the only operator we can use to access the members of an array via pointer notation using the array name. However, by defining a pointer to an array, we can get more flexibility to process the data in an array. For example, we can print the members of an array as follows:

#include<stdio.h> 

static const size_t N = 5   

int main() 

{    

size_t k = 0;    

int arr[] = {1, 2, 3, 4, 5};    

for(k = 0; k < N; k++)    

{        

printf("\n\t%d", *(arr + k));    

}    

return 0; 

}

By defining a pointer to the array, the above program is equivalent to the following:

#include<stdio.h> 

static const size_t N = 5   

int main() 

{    

size_t k = 0;    

int arr[] = {1, 2, 3, 4, 5};    

int *ptr = arr; /* or int *ptr = &arr[0]; */    

for(k = 0; k < N; k++)    

{        

printf("\n\t%d", ptr[k]);        

/* or   printf("\n\t%d", *(ptr + k)); */        

/* or   printf("\n\t%d", *ptr++); */    

}    

return 0; 

}

See that the members of the array arr are accessed using the operators + and ++. The other operators that can be used with the pointer ptr are - and --.

Pointer subtraction

Given two pointers to the same type, evaluates into an object of type ptrdiff_t that holds the scalar value that must be added to the second pointer in order to obtain the value of the first pointer.

int arr[] = {1, 2, 3, 4, 5}; 

int *p = &arr[2]; 

int *q = &arr[3]; 

ptrdiff_t diff = q - p;

printf("q - p = %ti\n", diff); /* Outputs "1". */ 

printf("*(p + (q - p)) = %d\n", *(p + diff)); /* Outputs "4". */ 

Related questions

0 like 0 dislike
1 answer 5 views
0 like 0 dislike
1 answer 7 views
0 like 0 dislike
0 answers 14 views
0 like 0 dislike
1 answer 9 views
0 like 0 dislike
1 answer 9 views
  Realize your learning potential with courses starting at ₹ 420
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Online Free Training:  MACHINE LEARNINGPython Programming | Database Management System(DBMS) | Object Oriented Programming(OOPs) using C++ | Data Structures and Algorithms(DSA) | Java Programming | Linux/Unix | C Programming
Exams: List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...