I happened to miss a few classes before an exam, so while the class was going over pointers, I was busy studying for the exam. So I need help with the basics to pointers, I can't say I know even a little about them. About all I know is that you use a * before a variable name, but that only gives you like a current position right? I don't know! Well, our assignment is to take basically every point where we use an array index and to change it to a pointer, he recommended to make it easier, change any variable with index to ptr so int start_index; becomes int *start_ptr.
/*
Program sorts an array of integers using a selection sort.
The general algorithm repeatedly finds the smallest number
in the array and places it at the front of the list.
*/
using namespace std;
const int size = 20;
int find_small_index (int start_index, int numbers []);
void swap_values (int index1, int index2, int numbers []);
void print (int numbers []);
int main(int argc, char *argv[])
{
// array of numbers
int numbers [size] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41,
54, 128, 62, 44, 12, 1023, 89, 905, 32, -12};
int start_index; // current starting spot for search
int small_index; // index of the smallest number in the array
start_index = 0;
// continue finding the smallest value and placing it
// at the front of the list
while (start_index < size - 1)
{
small_index = find_small_index (start_index, numbers);
swap_values (small_index, start_index, numbers);
start_index++;
}
// finds and returns the index of the smallest number remaining in
// the array
int find_small_index (int start_index, int numbers [])
{
int small_index, // smallest index to be returned
index; // current index being viewed
small_index = start_index;
// look at each element
for (index = start_index + 1; index < size; index++)
// remember index of smaller value
if (numbers [index] < numbers [small_index])
small_index = index;
return small_index;
}
// swap the values in the array at indexes index1 and index2
void swap_values (int index1, int index2, int numbers [])
{
int swapper;
// prints the array in nice format, 10 numbers per line
void print (int numbers [])
{
int on_line, // number of values printed on the line
index; // index of current number being printed
on_line = 0;
// print each element in the array
for (index = 0; index < size; index++)
{
cout << setw (5) << numbers [index];
on_line++;
// if 10 numbers have been printed on the line
// go to next line
if (on_line == 10)
{
cout << "\n";
on_line = 0;
}
}
}
any kind of explanation or help would be greatly appreciated!!
However! In a nutshell, pointers are variables that store addresses in a computer's memory. Think of them as literally variables that point to things. You can get an address of a variable by prefixing a variable with an &, and you can get the memory at an address from a pointer by prefixing the pointer variable with a *.
Code snippets.
1 2 3 4 5 6 7 8 9 10 11 12
//Declare a pointer.
type_of_variable_pointer_points_to* name;
//Get the address of a compatible variable.
name = &variable_of_compatible_type_as_type_of_variable_pointer_points_to;
//Assign a value to the pointed-to variable.
*name = value;
//Get the address of the first element of an array.
name = array;
//Assign a value to the Nth element of an array.
name[N] = value;
//Another way of doing the above using pointer arithmetic.
*(name + N) = value;
Let us know if you have any more questions, alright?