I have this problem for an assignment and im lost on how to start. What really gets me are the two pointers - referencing them to the address of the last character of the string and swapping the characters...on that note, what purpose do pointers serve? I know they "point" to addresses in memory, but as a beginner doing not so complicated programs, I can't see their usage being needed...how are they beneficial in more advanced programs?
"Write a program that accepts a C-string input from the user and reverses the contents of the string. Your program should work by using two pointers. The “head” pointer should be set to the address of the first character in the string, and the “tail” pointer should be set to the address of the last character in the string (i.e., the character before the terminating null). The program should swap the characters referenced by these pointers, increment “head” to point to the next character, decrement “tail” to point to the second-to-last character, and so on, until all characters have been swapped and the entire string reversed"
Thanks
(PS: I'm not looking for a solution, merely to be steered in the right direction)
And for pointers...they are useful for dynamically creating objects/memory, as well as keeping track of the same object from lots of places. They are also important for polymorphism.
1 2 3 4 5 6 7
unsignedint num_entries = 0;
DataEntry* entries = NULL;
std::cout<<"How many entries do you want to input? ";
std::cin>>num_entries; //yeah, I'm ignoring error checking here for simplicity
entries = new DataEntry[num_entries];
//do stuff with it