Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
good evening,

i'm a student currently learning how to code using C++. it is ok if I ask how Pointers is useful in making programs? I find kinda difficult to appreciate the use of pointers back when I was learning the C language. can someone show me or tell me the use of pointers in some problems?

some questions that may help me understand.
1. can you give me a simple sample program in which it is required to use a pointer?
2. can you give me a sample program that shows the difference of using pointers and not using a pointer?


I think that the second question would help me appreciate the use of pointers.

thank you.

sorry if I asked a question that is too basic for a real programmer. still trying to learn :) thank you.

PS: if there is anything wrong with this post. Please tell me. like if this kinds of post is against the rules of this site.
Posted
Comments
Albert Holguin 16-Feb-15 8:43am    
Answering as comment since I don't feel like making a real detailed answer. Every program has pointers, since it's the pointer to data in memory (you may not realize they're there... but they're always there). As to when it's useful, well, the alternative is to make copies every time you pass data along, which is terribly inefficient and depending on a few factors, slow.

Pointers is a fundamentals data type - see it as address to some memory of your PC. Now EVERYTHING piece of data in your PC is a pointer. :-O

Pointers are a fine and powerful tool to manipulate data: direct and fact execution without overhead. But with power comes responsebility.

In multimedia programming like audio or video it is a must to work with pointers to reach a fine performance. Even so in networking code.

You may interested to read the article Pointers Usage in C++: Beginners to Advanced .
 
Share this answer
 
Pointers are something you meet in the real world all the time - just you don't recognise them.
For example, if you want to watch something on TV, you can pick up a TV listing magazine and look at what is on now - you get a list of programs that are currently showing. Those aren't the program itself, they are "pointers" to the channel, and time, and a description of the program. To use the listed item, you look at the "pointer to the channel" - the channel name, or maybe number - and use that to reference the actual program via your TV.

A filing system is another example (and one that translates to computers at lot more easily). You don't store everything in one file in a cabinet, you store them by customer name in separate files so you can find all the related information together. All you have to do is use the customer name as a pointer to the right file and you have the data.

Computers do this all the time: memory is just a long list of locations that can store information, and each location has an address. A pointer just stores the address instead of the value at the address. This means that you can have several places in your code pointing at the same place instead of repeating the information - so if a customer changes his address if gets altered in one location, but all the places in your app that use the address get updated!

Take an example: you have a string from the user, and you want to truncate it at the first "dot" character: '.'

You could refer to each character in the string by name:
C++
if (char1 == '.') char1 = '\0';
if (char2 == '.') char2 = '\0';
if (char3 == '.') char3 = '\0';
if (char4 == '.') char4 = '\0';
...
but that gets very difficult when you don't know how long the input is, or its a very long string.
Instead you could just use a pointer in a loop:
C++
for (i = 0, pstr = startOfString; i < lengthOfString; i++, pstr++)
   {
   if (*pstr == '.')
      {
      *pstr = '\0';
      break;
      }
   }
Now you don't need to worry about individual names, or about how long the string is.
 
Share this answer
 
Comments
Reuben Cabrera 16-Feb-15 9:19am    
I have this program done by a classmate.

#include <iostream>

using namespace std;

void shiftArray(int* anArray, int aStart, int anEnd);
int getLargestInArray(int * anArray, int aSize);
void selectionSort(int* anArray, int aSize);


void printArray(int* anArray, int aSize){
cout<<"\n";
for (int i=aSize-1; i>=0 ; i--){
cout<<anArray[i]<<endl;
}

}


int getLargestInArray(int* anArray, int aSize){
int iHighest = 0;
for(int i=0; i<asize; i++){
="" if(anarray[ihighest]<anarray[i]){
="" ihighest="i;
" }
="" }

="" return="" ihighest;
}


void="" selectionsort(int*="" anarray,="" int="" asize){
="" ipos,="" ikey;
="" for(int="" i="aSize;">=0; i--){
iPos = getLargestInArray(anArray, i);
iKey = anArray[iPos];
//cout << iKey;

shiftArray(anArray, iPos, i);
anArray[i - 1] = iKey;
}


}
void shiftArray(int* anArray, int aStart, int anEnd){
for(int i=aStart; i<anend; i++){
="" anarray[i]="anArray[i+1];
" }
}


int="" main(){
="" int="" iinput,icount="0;
" cout&lt;&lt;"how="" many="" numbers:"="" ;
="" cin="">> iInput;
int array[iInput];

while(iCount<iinput){
cin="">> array[iCount];
iCount++;
}

selectionSort(array, iCount);
printArray(array, iCount);
return 0;
}


why is there a need to use pointers in this program? can this program be somehow be done without the use of pointers?
Reuben Cabrera 16-Feb-15 9:42am    
ok here is another example.

with pointers:
#include<iostream>
#include<string>

using namespace std;

void function1(int a){
cout <<"you entered"<< a
<<"so function1 was called\n\n";
}

void function2(int b){
cout<<"you entered " << b
<< "so function2 was called\n\n";
}

void function3(int c){
cout<<"you entered " << c
<< "so function3 was called\n\n";
}




int main(){

void (*f[3])(int) = {function1,function2,function3};

int choice;

while(true){

cout <<"Enter a Number between 0 and 2, 3 to end:";
cin >> choice;
if(choice < 0 || choice>=3)
break;
(*f[choice])(choice);
}
cout << "Program Execution Completed." << endl;
}


i deleted the *. then when I compiled the code, it produced errors say declaration of f as array of functions.

btw, what does this line mean??
(*f[choice])(choice);

void (*f[3])(int) = {function1,function2,function3}; <---- isnt this suppose to be outside of main???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900