0

Hey guys, I "understand" pointers. But I do not understand how I could actually put them to use. If you could give me a few example programs of how they would be used, then I could try to make the programs. I don't want the code, just the logic behind the program. I don't someone to just hand me over the answer, it will be much more rewarding if I can figure it out on my own.

Thanks,

Joe

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by SpS
0

There are millions of examples because they are frequently used in every-day applications. But a simple introduction might be to write a function that sets all the bytes of a character array to 0. Function prototype might be

void clear_array(char* array, int size);
0

Sample code

#include <iostream>

using namespace std;

int main()
{ 
  int x;            // A normal integer
  int *p;           // A pointer to an integer

  p = &x;           // Read it, "assign the address of x to p"
  cin>> x;          // Put a value in x, we could also use *p here
  cin.ignore();
  cout<< *p <<"\n"; // Note the use of the * to get the value
  cin.get();
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.