• The following code consists of 5 parts. • Find the errors for each part and fi
ID: 3850702 • Letter: #
Question
• The following code consists of 5 parts.
• Find the errors for each part and fix them.
• Explain the errors using comments on top of each part.
*/
#include <stdio.h>
#include <string.h>
int main( ) {
//////////////////////////////////////////////////////////////////////////
////////////// Part A. (5 points) //////////////////
int *number;
printf( "%d ", *number );
//////////////////////////////////////////////////////////////////////////
////////////// Part B. (5 points) //////////////////
float *realPtr;
long *integerPtr;
integerPtr = realPtr;
//////////////////////////////////////////////////////////////////////////
////////////// Part C. (5 points) //////////////////
int * x, y;
x = y;
//////////////////////////////////////////////////////////////////////////
////////////// Part D. (5 points) //////////////////
char s[] = "this is a character array";
int count;
for ( ; *s != ''; ++s)
printf( "%c ", *s );
//////////////////////////////////////////////////////////////////////////
////////////// Part E. (5 points) //////////////////
float x = 19.34;
float xPtr = &x;
printf( "%f ", xPtr );
return 0;
}
Explanation / Answer
Part A
int *number;
printf( "%d ", *number ); //Without initializing the number pointer we are tyring to print the value it points to.
Part B
float *realPtr;
long *integerPtr;
integerPtr = realPtr; //we have to type cast when assigning one type of pointer to another type of pointer.
/* Here inegerPtr is of type long and realPtr is of type float.*/
Part C
int * x, y;
x = y; //we are assignig inter value to the integer type pointer.
Part D
char s[] = "this is a character array";
int count;
for ( ; *s != ''; ++s) //++s here we are incrementing the address of the array.We can't increment the address of the array
printf( "%c ", *s );
Part E
float x = 19.34;
float xPtr = &x; /*Initializing the type float with float pointer.
xPtr is of float type and we can store only float values in xPtr,but here we are trying to store address the x which is of float type
*/
printf( "%f ", xPtr );
return 0;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.