• edited by
262 views

1 Answer

1 1 vote

The output of the program is 4. In the test function, the pointer p starts at the beginning of the array {4, 8, 5, 13}. The while(*p++ != 13); loop uses the post-increment operator, which means it first retrieves the value at the current address to check if it's 13 and then immediately moves the pointer to the next memory address. When the loop finally encounters the value 13, the condition becomes false and the loop stops, but the pointer p still performs that final increment. This leaves p pointing exactly four positions ahead of the starting pointer a (one position past the 13). Therefore, the pointer subtraction p - a calculates the distance between these two addresses, which is 4.

 

Position:
Show:

Related questions

2 2 votes
1 answers 1 answer
5.0k
5.0k views
Shubhanshu asked Jul 11, 2017
4,969 views
Consider the program:-void main(){ int a[] = {0, 1, 2, 3, 4}; int *p[] = { a, a+1, a+2, a+3, a+4 }; int ptr = p; ptr++; print( ptr - p, *ptr - p, ptr ); *ptr++; print( ...
8 8 votes
1 1 answer
240
240 views
GO Classes asked Jun 19
240 views
What is the output of the following code?#include <stdio.h int main() { int a[] = {10, 20, 30}; int *p = a; printf("%d ", *p++); printf("%d ", *p); printf("%d", ++*p); re...
8 8 votes
1 1 answer
283
283 views
GO Classes asked Jun 19
283 views
What is the output of the following code?#include <stdio.h void fun(int a[], int *p) { a = a + 5; *(p + 2) = *(p + 2) + 10; p++; *p = *p + 1; } int main() { int arr[] =...
6 6 votes
1 1 answer
229
229 views
GO Classes asked Jun 19
229 views
What is the output of the following code?#include <stdio.h int main() { int arr[] = {10, 20, 30}; int *p = arr; printf("%d %d", *p, *(arr + 2)); return 0; }$\texttt{10 20...