Tanvi Tanvi - 3 months ago 13
C Question

array passed as array and pointer-C

Why does this program produce

p
and
q
as outputs? What is the difference between passing an array as a pointer or as an array.

#include<stdio.h>

void fun(char i[]){
printf("%c,%c", i[1],i[2]);
}
void fun2(char *i){
printf("\n%c,%c", i,i+1);
}
int main(){
char ar[] = {"Aba"};
fun(ar);
fun2(ar);
return 0;
}


Output:

b,a
p,q

Answer

You are printing the ASCII conversion of a pointer address in the second function. You must dereference the pointer via *i and *(i+1).

Comments