![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| I always confuse the usage of * in C++. I think sometime it uses as pointer and sometime it uses as array. I get trouble with these usage. For Example: void tokenize(char** name,int& num,char* buffer); In this sentence char** name is for pointer or double array? I would like to know clearly the usage of pointer, reference(&) and array clearly.Thanks for every body who reply me. LA |
| |
| The Following User Says Thank You to MrPickle For This Useful Post: | ||
lwinaung (07-19-2008) | ||
| |||
| the * symbol, when used in conjunction with a data type is always a pointer, and never an array. This rule goes without exception. Unfortunately, C and C++ allow pointer syntax and array syntax to be mixed freely, allowing a programmer to create rather deceptive code which, on the face of it, creates a blurry distinction between arrays and pointers. Often, a pointer variable is used to store the address of the first element in an array (Which can be used to calculate subsequent elements). Here is an example where pointer syntax and array syntax can be used interchangably, yet, on the face of it, you could be forgiven for thinking that the two are completely different Code: #include <iostream>
int main()
{
int my_array[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500 };
int* my_pointer;
my_pointer = my_array;
std::cout << my_pointer[0] << std::endl;
std::cout << *my_pointer << std::endl;
} Quote:
Just to prove that i'm not making this up, here's the same scenario involving the 4th element in the array Code: #include <iostream>
int main()
{
int my_array[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500 };
int* my_pointer;
my_pointer = my_array;
std::cout << my_pointer[3] << std::endl;
std::cout << *(my_pointer+3) << std::endl;
} - Pointer arithmetic is a facility which C/C++ provides in order to take any address in memory, and step over a certain number of bytes in order to reach some other address. The smallest number of bytes which you may step over in any single calculation is exactly equal to the sizeof the pointed-to object. - for int* the "step size" in bytes is sizeof(int) - for char* the "step size" in bytes is sizeof(char) etc So, remembering that my_pointer merely stores an address in memory - the program's code shows that my_pointer has type int*. When adding 3 to the address stored in my_pointer, the final address value will be the value of my_pointer plus 3 lots of sizeof(int) if my_pointer happened to be an address value of 02000 (decimal), and sizeof(int) happened to be 4, the final address value would be 02012 decimal (Although its extremely rare to actually need to know, or care, what the absolute address values are, since memory is allocated at runtime) - Back on topic as to how this fits in with arrays; The most important feature of an array is that arrays are contiguous memory blocks. This is important, because, if you know the size of the individual elements, and you know the address of the first element, then pointer arithmetic empowers you to calculate the addresses of all other elements within your array. This is essentially how your CPU 'knows' where each element of an array is - But, luckily, C and C++ don't require the programmer to get involved, because subscript notation does it all for you! |
![]() |
| Thread Tools | |
| Display Modes | |
| |