1. int *ptr ; What are we doing here ? 2. int x = 10; int *ptr = x ; printf (\"
ID: 3567113 • Letter: 1
Question
1.
int *ptr ;
What are we doing here ?
2. int x = 10;
int *ptr = x ;
printf (" %d ", *ptr) ;
A programmer wrote the above code in an application. Is there a problem in the code ?
3.
Are these two version of the code work the same or different?
Version 1:
int x = 10;
int y = x;
version 2:
int x = 10;
int *ptr = &x ;
int y = *ptr ;
4.
if
int *ptr = x ;
then the variable x should be defined as
a. int x;
b. float x;
c. int *x;
d. All of the above
5.
int *x ;
int iA [ 4 ] ;
iA = x ;
Is the above statement valid ?
Consider this code
int *x;
int iA [ 4 ] ;
6.
x = iA ;
To which cell is x pointing to ?
7. In the above program clip,
x = ( iA + 3 ) ;
Now, to which cell does x points to ?
8.
in the same code above, if we change
x = iA + 5;
Is the above statement (though valid), result in good working code ?
9.
using the same code above, Consider this
x = iA ;
we now want to print all values in the array using a for loop.
how would you print all values using the pointer x
10.
unsigned char *ptr ;
unsigned int iA [ 2 ] = { 0x01020304, 0x05060708} ; // integer array
ptr = iA ; // we made char pointer point to a integer array
ptr = iA + 1 ;
printf (" %d ", *ptr);
What would be printed ?
Explanation / Answer
1.int *ptr ;
Ans: Here we declaring pointer of integer type.
2. int x = 10;
int *ptr = x ;
printf (" %d ", *ptr) ;
Ans: Pointer always store the address of value.
So there is a error in second line.
Correct Statement is: int *ptr = &x;
3.Are these two version of the code work the same or different?
Version 1:
int x = 10;
int y = x;
version 2:
int x = 10;
int *ptr = &x ;
int y = *ptr ;
Ans: Both are same.
4.if
int *ptr = x ;
then the variable x should be defined as
a. int x;
b. float x;
c. int *x;
d. All of the above
Ans: int x.
5.
int *x ;
int iA [ 4 ] ;
iA = x ;
Is the above statement valid ?
Consider this code
int *x;
int iA [ 4 ] ;
Ans: No the above code is wrong.
Right code:
int *x ;
int iA [ 4 ] ;
x=iA ;
6.
x = iA ;
To which cell is x pointing to ?
Ans:First cell i.e iA[0].
7. In the above program clip,
x = ( iA + 3 ) ;
Now, to which cell does x points to ?
Ans:iA[3].
8.
in the same code above, if we change
x = iA + 5;
Is the above statement (though valid), result in good working code ?
Ans: Invalid, it will give error.
9.
using the same code above, Consider this
x = iA ;
we now want to print all values in the array using a for loop.
how would you print all values using the pointer x
Ans: for(i=0;i<4;i++)
{
print("%d",*(x+i));
}
10.
unsigned char *ptr ;
unsigned int iA [ 2 ] = { 0x01020304, 0x05060708} ; // integer array
ptr = iA ; // we made char pointer point to a integer array
ptr = iA + 1 ;
printf (" %d ", *ptr);
What would be printed ?
Ans:0x05060708
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.