|
-
August 17th, 2009, 09:56 PM
#1
Beginning C++, Pointer problem
I've got the basics down pat for pointers somewhat, I still don't understand the real purpose of a pointer. I know how to declare a basic pointer and difference it. I have some understanding of pointers with arrays and limited understanding of any sort of pointer arithmetic.
I have a 1,500 page book by Ivor Horton Visual C++ 2008, I purchased it because it's easy to understand and it covers most all areas including CLI / Win32 / and the other M??.
It's getting very advanced with pointers to functions and I already have limited knowledge of pointers, can someone help me out at all and clarify all this. I still don't even understand the point of pointers their really just variables that reference the value by absolute physical path rather than through the processor
-
August 17th, 2009, 10:18 PM
#2
Re: Beginning C++, Pointer problem
Here's one of the uses of pointers (really simple). Say, you have an array of 20 int's that you initiated with values 1 through 20:
Code:
int array[20];
#define ARRAY_SIZE (sizeof(array) / sizeof(array[0]))
for(int i = 0; i < ARRAY_SIZE; i++)
{
array[i] = i + 1;
}
and you want to pass that whole array into a function (or sometimes it's also called "a method"). In this case it will be quite "expensive" CPU-clock-wise to pass all 20 values there. That is why you pass it by reference (which is only one value), for which you get a pointer to your array inside the function:
Code:
_tprintf(_T("The sum of all elements in array is %d"), SumOfAll(array, ARRAY_SIZE));
int SumOfAll(int* pArray, int ncbArraySize)
{
ASSERT(pArray);
int iSum = 0;
for(int i = 0; i < ncbArraySize; i++)
{
iSum += pArray[i];
}
return iSum;
}
Last edited by dc_2000; August 17th, 2009 at 10:21 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|