2,952 views
3 3 votes
#include<stdio.h>
int main()
{
    char arr[5][7][6];
    char *p[5][7][6];
    printf("%d\t", (unsigned)(arr+1)-(unsigned)arr);
    printf("%d", (unsigned)(p+1)-(unsigned)p);
}

Please enlighten about the 2nd printf statement. Also what is the difference between the two statements?

1 Answer

Best answer
6 6 votes

This is a really bad question -- because one is not allowed to typecast and address to long because an address can be 8 bytes long and "long" unsigned be even just 2 bytes. So,  this typecast can result in loss of precision and undesired results depending on the runtime address values. This proves that this question is from some coaching material and not any standard resource. Anyway I changed "unsigned" to "unsigned long" which is also not technically correct but I'm using it to explain some other points about pointer arithmetic. You should definitely try compiling and running the parts given in PS -- those who are lazy to do can skip it.

#include<stdio.h>
int main()
{
    char arr[5][7][6];
    char *p[5][7][6];
    printf("%d\t", (unsigned long) (arr+1)-(unsigned long)arr);
    //arr+1 gives the address location arr + sizeof(*arr)
    //(unsigned)(arr+1)-(unsigned)(arr) gives the size of (*arr) 
    // = 7*6*sizeof(char) = 42 
    printf("%d", (unsigned long)(p+1)-(unsigned long)p;
    //p+1 gives the address location p + sizeof(*p)
    //(unsigned)(p+1)-(unsigned)(p) gives the size of (*p) 
    // p is an array of pointers not pointer to an array
    // So, sizeof(*p) = 7 * 6 * sizeof(char*) = 336 on 64 bit compiler
}
PS: One should try removing the "unsigned long" typecast and see the result. 
Also try changing char *p[5][7][6] to char (*p)[5][7][6];

Ref: https://gatecse.in/chapter-3-pointers/

• selected by
Position:
Show:

Related questions

0 0 votes
1 1 answer
722
722 views
gatecse asked Dec 9, 2020
722 views
Match the following:$$\begin{array}{|l|l|l|l|} \hline (1) \text{Waterfall model} & (a) \text{Specifications can be developed} \\& \text{incrementally} \\\hline (2) \tex...
8 8 votes
1 answers 1 answer
7.4k
7.4k views
sh!va asked May 7, 2017
7,403 views
Estimation at software development effort for organic software in basic COCOMO is:E = 2.0 (KLOC) 1.05 PME = 3.4 (KLOC) 1.06 PME = 2.4 (KLOC) 1.05 PME = 2.4 (KLOC) 1.07...
2 2 votes
1 1 answer
782
782 views
Sugumaran asked Feb 7, 2018
782 views
#include<stdio.h int main() { int x=191; char *p; p=(char*)&x; printf("%d",*p); }explain it
3 3 votes
1 answers 1 answer
837
837 views
junaid ahmad asked Oct 31, 2017
837 views
OUTPUT OF THE FOLLOWING PROGRAMME#include<stdio.h int main() { int n1=10; int n2=15; int n3; n3=~n1; printf("%d",n3); return 0; }Basically here i want to understand how d...