Start a new file named Practice8.c Inside main do the following. Declare a point
ID: 3626068 • Letter: S
Question
Start a new file named Practice8.cInside main do the following.
Declare a pointer to an int named intPtr.
Do not make it point to anything yet.
Declare a pointer to a char named charPtr.
Do not make it point to anything yet.
Declare a pointer to a float named floatPtr.
Do not make it point to anything yet.
Put these line into main:
int arr[] = {18, 25, 15, 19, 30, 31, 27}; // this array uses 28 bytes of memory
char brd[] = "Wunderboard is wunderbar"; // 12 bytes =11 letters + null terminator char
float grades[] = {29.5, 30, 26.5, 28, 19.25};
Assign intPtr to point to the 31 in arr.
Add this print statement to test:
printf("*intPtr = %i ", *intPtr);
Assign charPtr to point to the 'w' in brd. Not 'W'.
Add this print statement to test:
printf("should print wunderbar: %s ", charPtr);
Assign floatPtr to point to the 26.5 in grades.
Add these print statements to test:
printf("floatPtr[0] = %f, *floatPtr = %f ", floatPtr[0], *floatPtr);
printf("floatPtr[1] = %f, *(floatPtr+1) = %f ", floatPtr[1], *(floatPtr+1));
printf("floatPtr[2] = %f, *(floatPtr+2) = %f ", floatPtr[2], *(floatPtr+2));
Declare a ptr to an int named ptr.
Assign ptr to equal intPtr.
Test with this print statement to test:
printf("*ptr = %i ", *ptr);
ptr should be pointing to the 31 in the array named arr. Write an assignment statement so that ptr points to 98. (This means the 31 is overwritten with 98.)
Write a for loop that prints all 7 ints in arr. Remember, arrays start at index 0. Your output should look like this:
arr[0] = 18
arr[1] = 25
arr[2] = 15
arr[3] = 19
arr[4] = 30
arr[5] = 98
arr[6] = 27
Explanation / Answer
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
int*intPtr;
char *charPtr;
float *floatPtr;
int arr[] = {18, 25, 15, 19, 30, 31, 27}; // this array uses 28 bytes of memory
char brd[] = "Wunderboard is wunderbar"; // 12 bytes =11 letters + null terminator char
float grades[] = {29.5, 30, 26.5, 28, 19.25};
intPtr = &arr[5];
printf("*intPtr = %i ", *intPtr);
charPtr = &brd[15];
printf("should print wunderbar: %s ", charPtr);
floatPtr = &grades[2];
printf("floatPtr[0] = %f, *floatPtr = %f ", floatPtr[0], *floatPtr);
printf("floatPtr[1] = %f, *(floatPtr+1) = %f ", floatPtr[1], *(floatPtr+1));
printf("floatPtr[2] = %f, *(floatPtr+2) = %f ", floatPtr[2], *(floatPtr+2));
int *ptr;
ptr = intPtr;
printf("*ptr = %i ", *ptr);
//printf("*ptr = %i ", *ptr);
*ptr = 98;
for(i=0; i<7; i++)
printf("arr[%d] = %d ",i,*(ptr-5+i));
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.