In this lab, we want to get some practice with pointers -- specifically, the basic syntax of declaring them, storing memory addresses into them, dereferencing them, and seeing how they work with pointer arithmetic in the context of built-in, C-style arrays.
Unless specified otherwise, you can use any valid variable names you wish. The requirements for this program are as follows:
Part A
Part B
Part C
Sample output is provided below. Note that your output will vary, depending on the values you chose to store... the important thing here is the syntax you used to produce the output.
Value stored in x: 121
Value stored in y: 2.7
Using array subscript notation: p
Using pointer offset notation with array name: p
Using pointer subscript notation: p
Using pointer offset notation with pointer name: p
C++
#include<stdio.h>
int main(){
// Part A
int *p1;
int x=121;
p1=&x;
printf("Value stored in x %d \n",*p1);
// Part B
double y=2.7;
double *p2=&y;
printf("Value stored in y %f \n",*p2);
// Part C
char z[]={'a', 'v', 'g', 'x', 'p'};
char *p3=z;
printf("Using array subscript notation: %c \n",z[4]);
printf("Using pointer offset notation with array name: %c \n",*(p3+4));
printf("Using pointer subscript notation: %c \n",p3[4]);
printf("Using pointer offset notation with pointer name: %c \n",*(z+4));
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.