Qualcomm Interview Question
Software Engineer / DevelopersIf four was expected, then the first print statement would need to be like:
printf("%d\n", (int) ( (char*)(p+1) - (char*)p ) );
Judging from the code and question, it seems like the desired result was to print out the size of int and char.
even with this:
int *p,*p11,pp[]={11,22,33};
char *q,*q11,*qq="qqq";
p=pp;
q=qq;
printf("--->1st: %d\n",p+1-p);
printf("--->2nd: %d\n",q+1-q);
p11=p+1;
q11=q+1;
printf("--->1st: %d\n",p11-p);
printf("--->2nd: %d\n",q11-q);
p11=pp;
q11=qq;
p11++;
q11++;
printf("--->1st: %d\n",p11-pp);
printf("--->2nd: %d\n",q11-qq);
--->1st: 1
--->2nd: 1
--->1st: 1
--->2nd: 1
--->1st: 1
--->2nd: 1
The reason is when you do pointer subtraction it gives you the no of positions/elements by/in which the two pointers differ. Rather than giving the mathematical differnce (which someone can think to be 4 for the first case.)
- 123 February 15, 2010