Question
1) Compile, run, and study this C program: #include int main(void){ // note the array declaration format is slightly different from Java int myInts[] =
1) Compile, run, and study this C program:
#include
int main(void){
// note the array declaration format is slightly different from Java
int myInts[] = {0, 1, 2, 3, 4};
// declare an int pointer called pi and set it to point to the beginning of the array
// note that an array variable is a pointer to the beginning of the array
int *pi = myInts;
for(int counter = 0; counter
/* printf is also available in Java, but you may never have used it.
printf uses formatting codes like %p for pointer (addresses expressed in bytes in hexadecimal)
and %d for (decimal) int
*/
printf("Address %p: Value %d ", pi, *pi);
// incrementing a pointer changes it by the size of the type to which it points
pi += 1;
}
}
2) Add to this C program as follows:
a) Create an array of any five doubles and use a double pointer to iterate through it and print the addresses and values in the same way as the sample code iterates through the int array. The printf format code for a double is %f.
b) Do the same with an array of any five chars (for example, char myChars = {'J', 'o', 'h', 'n'}. The format code for a char is %c.
c) Add a multi-line comment (same syntax as Java) answering this question:
Suppose you had no other way to find out the size of an int, a double, or a byte (and note that, in C, these may actually vary between different compilers!) How could the output from your program help you answer these questions?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started