C Programming - Strings - Discussion

2. 

For the following statements will arr[3] and ptr[3] fetch the same character?
char arr[] = "IndiaBIX";
char *ptr = "IndiaBIX";

[A]. Yes
[B]. No

Answer: Option A

Explanation:

Yes, both the statements prints the same character 'i'.


Vikas Kumar said: (Jun 17, 2011)  
I think answer is wrong... because ptr[3] will print the addres of the 'i'..

Reply me soon..

Sundar said: (Jun 17, 2011)  
The given answer is correct. I have tested the following code:

#include<stdio.h>
int main()
{
char arr[] = "IndiaBIX";
char *ptr = "IndiaBIX";

printf("\n arr[3] = %c", arr[3]);
printf("\n ptr[3] = %c", ptr[3]);

return 0;
}

//output:
arr[3] = i
ptr[3] = i

Rupinder said: (Nov 25, 2011)  
Here are two different techniques to point to the any of the array elements.
#1:Array Indexing(arr[3])
#2: Pointer arithmetic(ptr[3]==>*(ptr+3))

Read the pointer arithmetic concept, you'll be able sort this out by your own.

Karthika said: (Jun 22, 2016)  
But I have a doubt *ptr gives I as output then it means it is not a pointer but that is not the case with C. So please clear my doubt.

Waghmare N.I. said: (Aug 1, 2016)  
Yes, the answer is written after executing code but the pointer is used to store the address of a variable in the memory.

And here pointer have size 3 and array have 3 character string. But the pointer addresses.

I am confused about the problem please solve this confusion.

Pranali said: (Apr 1, 2017)  
@All.

In arr[] and ptr we are storing same value/string. And we are accessing 3rd element from both the array and pointer. so it will print 'i'.

As we access each and every element from array by saying, arr[0],arr[1], etc.
same like that pointer works as ptr[0],ptr[1], etc.


Understand the concept of pointer arithmetic, then you will get it.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.