Question
Part II. Pointers and Arrays 4. Pointers and arrays: Now, starting from the (original) basic program create a pointer, initialize it to the beginning of
Part II. Pointers and Arrays
4. Pointers and arrays: Now, starting from the (original) basic program
create a pointer, initialize it to the beginning of the array data
output the elements of data by indexing the array data[ ] using a loop (and the indexing operator []).
output the elements of data by indexing your pointer using a loop, with the pointer pointed at the beginning of the array and using [ ]
output the elements of data by incrementing the pointer each time through the loop. your loop control could look like this for (i=0; i < 5; i++) { p++; } or like this (why does this work?): for (p=data; p < data+5; p++)
reset your pointer to point at the beginning of the array again, then output the elements of data by adding i, a loop counter, to the pointer and dereferencing the summed address. Use something like: cout << *(p+i) (Be sure to point back at the beginning of the array before the loop!)
Q: how is cout << *(p+i) different from cout << *p+i ? If you're not sure, try both and see what happens. To verify your answer, look at the operator precedence table in the textbook and find the precedence of * (for dereferencing) and +. Which is higher precedence?
Name your program pointers_arrays.cpp
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