| Arjun Suresh (talk | contribs) | Arjun Suresh (talk | contribs)  | ||
| (4 intermediate revisions by the same user not shown) | |||
| Line 88: | Line 88: | ||
| { | { | ||
|    int *a = (int*) 1; |    int *a = (int*) 1; | ||
| − | + |   printf("%d", a); | |
| } | } | ||
| </syntaxhighlight> | </syntaxhighlight> | ||
| |type="()" | |type="()" | ||
| /} | /} | ||
| − | - | + | -Garbage value | 
| +1 | +1 | ||
| -0 | -0 | ||
| Line 107: | Line 107: | ||
| { | { | ||
|    int a = 1, *p, **pp; |    int a = 1, *p, **pp; | ||
| − | + |   p = &a; | |
| − | + |   pp = p; | |
| − | + |   printf("%d", **pp); | |
| } | } | ||
| </syntaxhighlight> | </syntaxhighlight> | ||
| |type="()" | |type="()" | ||
| /} | /} | ||
| + | -Garbage value | ||
| -1 | -1 | ||
| − | |||
| -Compile error | -Compile error | ||
| +Segmentation fault | +Segmentation fault | ||
| Line 127: | Line 127: | ||
| { | { | ||
|    int a = 1, *p, **pp; |    int a = 1, *p, **pp; | ||
| − | + |   p = &a; | |
| − | + |   pp = p; | |
| − | + |   printf("%d", *pp); | |
| } | } | ||
| </syntaxhighlight> | </syntaxhighlight> | ||
| Line 135: | Line 135: | ||
| /} | /} | ||
| +1 | +1 | ||
| − | - | + | -Garbage value | 
| -Compile error | -Compile error | ||
| -Segmentation fault | -Segmentation fault | ||
<quiz display="simple"> {What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
 int a = 5;
 int* p = &a;
 printf("%d", ++*p);
} </syntaxhighlight> |type="{}" /} { 6 } ||p is pointing to the address of a. *p will return the content of a which is 5 and ++ will increment it to 6.
{What will be the output of the following code?
<syntaxhighlight lang="c">
int main() {
 char a[] = "Hello World";
 char* p = &a;
 printf("%s", p+2 );
} </syntaxhighlight> |type="()" /} -Compiler Error -World +llo World -Runtime Error
||Since p is a char pointer p+2 will add 2 to p (since sizeof(char) is 1). So, p+2 will be pointing to the string "llo World".
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
 int a;
 int* p = &a;
 printf("%zu", sizeof( *(char*)p  ));
} </syntaxhighlight> |type="()" /}
+1 -2 -4 -Compile Error
||p is typecasted to char pointer and then dereferenced. So, returned type will be char and sizeof(char) is 1.
{Is the following code legal? <syntaxhighlight lang="c">
int main() {
 int a = 1;
 if((char*)  &a)
 {
   printf("My machine is little endian");
 }
 else
 {
    printf("My machine is big endian\n");
 }
} </syntaxhighlight> |type="()" /} +Yes -No ||On a little endian machine the lower address will contain the least significant byte. Suppose a is stored at address 1000 and contains 1, then character at 1000 will be 1, if the machine is little endian.
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
 int *a = (int*) 1;
 printf("%d", a);
} </syntaxhighlight> |type="()" /} -Garbage value +1 -0 -Compile error -Segmentation fault ||Assigning int values to pointer variable is possible. Only when we dereference the variable using *, we get a segmentation fault.
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
 int a = 1, *p, **pp;
 p = &a;
 pp = p;
 printf("%d", **pp);
} </syntaxhighlight> |type="()" /} -Garbage value -1 -Compile error +Segmentation fault ||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a, but **p will use 1 as an address and tries to access the memory location 1, giving segmentation fault.
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
 int a = 1, *p, **pp;
 p = &a;
 pp = p;
 printf("%d", *pp);
} </syntaxhighlight> |type="()" /} +1 -Garbage value -Compile error -Segmentation fault ||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a.
{Assuming a little endian machine, what will be the output of the following program?
<syntaxhighlight lang="c">
fun(int a) {
 char *arr[] = {"0000", "0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
 unsigned char* p = (unsigned char*) &a ;
 p+=3;
 int i;
 for(i = 0; i < sizeof a; i++)
 {
   int d = (*p)>>4; 
   printf("%s", arr[d]);
   d = (*p) & 0xf;
   printf("%s ", arr[d]);
   p--;
 }
}
int main() {
int a;
scanf("%d", &a);
fun(a);
} </syntaxhighlight> |type="()" /} +Print the binary of the input number -Compile error -Runtime error -Compiler dependent output ||The code is printing the binary equivalent of the input number. Suppose a is stored starting from address 1000. Since, we assume a little endian machine, the LSB of a will be stored at address 1000 and MSB will be stored at address 1003. So, we make a char pointer to 1003 and take out the MSB. Then using shift operator we get the most significant 4 bits from it and then the lest significant 4 bits. We repeat the same for the other three bytes of a. </quiz>
<quiz display="simple"> {What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
 int a = 5;
 int* p = &a;
 printf("%d", ++*p);
} </syntaxhighlight> |type="{}" /} { 6 } ||p is pointing to the address of a. *p will return the content of a which is 5 and ++ will increment it to 6.
{What will be the output of the following code?
<syntaxhighlight lang="c">
int main() {
 char a[] = "Hello World";
 char* p = &a;
 printf("%s", p+2 );
} </syntaxhighlight> |type="()" /} -Compiler Error -World +llo World -Runtime Error
||Since p is a char pointer p+2 will add 2 to p (since sizeof(char) is 1). So, p+2 will be pointing to the string "llo World".
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
 int a;
 int* p = &a;
 printf("%zu", sizeof( *(char*)p  ));
} </syntaxhighlight> |type="()" /}
+1 -2 -4 -Compile Error
||p is typecasted to char pointer and then dereferenced. So, returned type will be char and sizeof(char) is 1.
{Is the following code legal? <syntaxhighlight lang="c">
int main() {
 int a = 1;
 if((char*)  &a)
 {
   printf("My machine is little endian");
 }
 else
 {
    printf("My machine is big endian\n");
 }
} </syntaxhighlight> |type="()" /} +Yes -No ||On a little endian machine the lower address will contain the least significant byte. Suppose a is stored at address 1000 and contains 1, then character at 1000 will be 1, if the machine is little endian.
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
 int *a = (int*) 1;
printf("%d", a);
} </syntaxhighlight> |type="()" /} -0 +1 -0 -Compile error -Segmentation fault ||Assigning int values to pointer variable is possible. Only when we dereference the variable using *, we get a segmentation fault.
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
 int a = 1, *p, **pp;
p = &a;
pp = p;
printf("%d", **pp);
} </syntaxhighlight> |type="()" /} -1
-0
-Compile error +Segmentation fault ||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a, but **p will use 1 as an address and tries to access the memory location 1, giving segmentation fault.
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
 int a = 1, *p, **pp;
p = &a;
pp = p;
printf("%d", *pp);
} </syntaxhighlight> |type="()" /} +1 -0 -Compile error -Segmentation fault ||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a.
{Assuming a little endian machine, what will be the output of the following program?
<syntaxhighlight lang="c">
fun(int a) {
 char *arr[] = {"0000", "0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
 unsigned char* p = (unsigned char*) &a ;
 p+=3;
 int i;
 for(i = 0; i < sizeof a; i++)
 {
   int d = (*p)>>4; 
   printf("%s", arr[d]);
   d = (*p) & 0xf;
   printf("%s ", arr[d]);
   p--;
 }
}
int main() {
int a;
scanf("%d", &a);
fun(a);
} </syntaxhighlight> |type="()" /} +Print the binary of the input number -Compile error -Runtime error -Compiler dependent output ||The code is printing the binary equivalent of the input number. Suppose a is stored starting from address 1000. Since, we assume a little endian machine, the LSB of a will be stored at address 1000 and MSB will be stored at address 1003. So, we make a char pointer to 1003 and take out the MSB. Then using shift operator we get the most significant 4 bits from it and then the lest significant 4 bits. We repeat the same for the other three bytes of a. </quiz>