Click here to Skip to main content
16,010,139 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralMFC CString 4.0 vs MFC CString 6.0 Pin
28-Dec-00 4:27
suss28-Dec-00 4:27 
GeneralRe: MFC CString 4.0 vs MFC CString 6.0 Pin
Erik Funkenbusch28-Dec-00 13:02
Erik Funkenbusch28-Dec-00 13:02 
GeneralMissing messages Pin
28-Dec-00 1:40
suss28-Dec-00 1:40 
GeneralAnyone knows how to write into Output toolbar (something like TRACE does only in some other tab like Result) of MSVC++ Pin
27-Dec-00 21:30
suss27-Dec-00 21:30 
GeneralRe: Anyone knows how to write into Output toolbar (something like TRACE does only in some other tab like Result) of MSVC++ Pin
J Patel3-Jan-01 5:43
J Patel3-Jan-01 5:43 
QuestionWhat are pointers for??? Pin
27-Dec-00 15:09
suss27-Dec-00 15:09 
AnswerRe: What are pointers for??? Pin
Tim Deveaux28-Dec-00 4:21
Tim Deveaux28-Dec-00 4:21 
AnswerRe: What are pointers for??? Pin
Alvaro Mendez28-Dec-00 5:23
Alvaro Mendez28-Dec-00 5:23 
You ask questions that are better answered by a book on C programming. However, I will do my best to give you an accurate answer.

A pointer is simply a variable whose value is the address of another variable. In the C language they serve two main purposes:

1. To represent arrays. In C, and array is a pointer to a block of memory which holds the elements contiguously. In fact, strings in C are represented as arrays of characters.

2. To pass a function parameter by reference. Whenever a function needs to modify a parameter, it does so using pointer semantics. Here's an example:

void zero(int* pN)
{
   *pN = 0;  // modify the value of the variable pointed to by pN
}
 
void foo()
{
   int n;
   zero(&n);  // pass the address of n
}


Notice that n is not a pointer but its address can be assigned to a pointer variable, which is what pN is.

So, as you can see, pointers are a necessity in C whenever you need to use arrays or pass parameters by reference. In contrast, C++ has objects and reference types which can greatly reduce your need to use pointers.

Now, as far as when to use pointers instead of simple object instanciations? For that you need to understand that simple object instanciations (on the call stack) are great if you only need the object to live within the scope of the block where you created it. If it needs to stay around longer, then you need to use pointers. Here's what I mean:


void foo()
{
   int n = 1;          // integer allocated on the stack
   
   int* pN = new int;  // integer allocated on the heap
   *pN = 1;
 
   keep(pN);   // GOOD - keep pN's value, which is what pN points to
   keep(&n);   // BAD - keep n's address, which is temporary
 
}  // n goes away here
   // pN goes away too what it points to remains on the heap (until delete is called)
 
void keep(int* pItem)
{
   // add pItem to some container in memory
}


So you see that this is a case where we need to hang on to the variable inside a container or something, then it needs to be allocated in the heap (with new) so it doesn't go away automatically like local variables do. The fact that we store the elements in the container by pointer also makes that very efficient in terms of speed and space. Pointers are only 4 bytes long (in Win32) and they can be copied as quickly as copying an integer. In contrast, if your container held its elements by value, it could take up more room and be less efficient to move elements in and out of it.

I hope I have shed some light to your pointer doubts. Again, it may help to complete the picture by picking up a good set of books on C/C++ programming.

Regards,
Alvaro
AnswerRe: What are pointers for??? Pin
Erik Funkenbusch28-Dec-00 13:09
Erik Funkenbusch28-Dec-00 13:09 
AnswerRe: What are pointers for??? Pin
29-Dec-00 0:02
suss29-Dec-00 0:02 
Generalat the risk of getting boring... Pin
l a u r e n27-Dec-00 4:46
l a u r e n27-Dec-00 4:46 
GeneralAceesing Structure From VC++\VB client To VC++ server Pin
27-Dec-00 2:44
suss27-Dec-00 2:44 
GeneralRe: Aceesing Structure From VC++\VB client To VC++ server Pin
l a u r e n27-Dec-00 4:48
l a u r e n27-Dec-00 4:48 
GeneralCListCtrl Questions - Again... Pin
#realJSOP27-Dec-00 1:47
professional#realJSOP27-Dec-00 1:47 
GeneralRe: CListCtrl Questions - Again... Pin
Alberto Bar-Noy27-Dec-00 2:05
Alberto Bar-Noy27-Dec-00 2:05 
GeneralRe: CListCtrl Questions - Again... Pin
l a u r e n27-Dec-00 4:52
l a u r e n27-Dec-00 4:52 
Questionhow can convert CEditView to CRichEditView? Pin
kykim26-Dec-00 20:28
kykim26-Dec-00 20:28 
GeneralAvi animatectl Pin
26-Dec-00 12:30
suss26-Dec-00 12:30 
GeneralRe: Avi animatectl Pin
Masoud Samimi26-Dec-00 13:15
Masoud Samimi26-Dec-00 13:15 
GeneralRe: Avi animatectl Pin
26-Dec-00 14:13
suss26-Dec-00 14:13 
GeneralRe: Avi animatectl Pin
Masoud Samimi27-Dec-00 18:16
Masoud Samimi27-Dec-00 18:16 
GeneralRe: Avi animatectl Pin
28-Dec-00 2:09
suss28-Dec-00 2:09 
GeneralRe: Avi animatectl Pin
Masoud Samimi28-Dec-00 2:29
Masoud Samimi28-Dec-00 2:29 
GeneralRe: Avi animatectl Pin
28-Dec-00 3:44
suss28-Dec-00 3:44 
GeneralRe: Avi animatectl Pin
Jim Howard3-Jan-01 10:25
Jim Howard3-Jan-01 10:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.