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

Recommended Answers

All 2 Replies

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);

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();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.