Finance[US] Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
234 views

1 Answer

0 like 0 dislike
by Goeduhub's Expert (9.3k 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". */ 

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

0 like 0 dislike
1 answer 582 views
0 like 0 dislike
1 answer 236 views
0 like 0 dislike
0 answers 234 views
0 like 0 dislike
1 answer 546 views
0 like 0 dislike
1 answer 301 views

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...