1. Explain the error.
char c = 'A';
double *p = &c;
2.
Give the value of the left-hand side variable in each assignment statement. Assume the lines are executed sequentially. Give brief explanation for each of your answer.
int main()
{
char blocks[3] = {'A','B','C'};
char *ptr = &blocks[0];
char temp;
temp = blocks[0];
temp = *(blocks + 2);
temp = *(ptr + 1);
temp = *ptr;
ptr = blocks + 1;
temp = *ptr;
temp = *(ptr + 1);
ptr = blocks;
temp = *++ptr;
temp = ++*ptr;
temp = *ptr++;
temp = *ptr;
return 0;
}
1)
c is declared as a charachter (char data type)
while declaring a pointer it is important for the compiler to know what data type it is pointing at
hence , data type of the pointer is always given as the data type of the variable which is to be pointed by the pointer
the correct code will be
char c = 'A';
char*p = &c;
2)
int main()
{
char blocks[3] = {'A','B','C'};
char *ptr = &blocks[0]; // address of first element of the array (address where A is stored)
// adding '&' at the start of any variable name will give the address of that variable
char temp;
temp = blocks[0]; // 'A' the value at zeroth position of the array is passed to temp
temp = *(blocks + 2); // 'C'
// the name of the array can be used to get the address of the first element of the array
// *blocks will return the value at zeroth position of the array
// if a * is added at the start of the variable name and the variable is an address of some other variable
// the value of the other variable will be returned
temp = *(ptr + 1); // 'B'
// ptr had the address of the first element of the array form the second line of main function
// adding 1 to the address of second element will give the adress of second element
.// * the address of second varable will return the second element of the array
temp = *ptr; // address of first element of the array
ptr = blocks + 1; // address of the second element
temp = *ptr; // 'B'
// as ptr was given the address of the second element , *ptr will give the contents of the address in ptr
temp = *(ptr + 1); // 'C'
// adding 1 to ptr which had the address of second element will give the address of third element
// adding a * to it will give the value at the address of third element ( which is third element itself)
ptr = blocks; // address of first element
temp = *++ptr; // 'B'
// ptr was given address of first element in previous line
// increment '++' at beginning means the increment the value first then execute rest of the line
// the value of ptr is incremented to next address i.e. address of second element
// * gives the value of second element
temp = ++*ptr; // 'C'
// *ptr will give 'B' but incrementation of 1 will make it C
// not because next element in the array is C
// its is due to the fact that adding 1 to ascii code of B will give the ascii code of C
// the value of ptr will still remain the address of the second element
temp = *ptr++; // B
// increment operater is after the variable name
// so the statement is executed first and then the incrementation takes place
// ptr had the address of the second element
// the value has updated to the address of third element after the execution of the statement
temp = *ptr; // C
// the value of ptr was updated to the address of the third element of the array after the execution of previous statement
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.