The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


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.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-18-2008, 02:27 AM
lwinaung lwinaung is offline
Novice
Join Date: Jul 2008
Posts: 8
iTrader: (0)
lwinaung is on a distinguished road
Icon6 Confusing with * for pointer or array in C++?

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

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 07-18-2008, 11:29 AM
MrPickle's Avatar
MrPickle MrPickle is offline
PT Admin
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 332
iTrader: (0)
MrPickle is a jewel in the roughMrPickle is a jewel in the roughMrPickle is a jewel in the rough
A pointer is used to point to a memory cell instead of creating a new variable at a new memory cell. This means that the pointer is always the value of whatever the memory cell's value is.

Now, each memory cell of our computer has it's own address, like your street or an estate for example. Now when you use & it gets the address of the variable instead of the value.

Try doing this for example:
Code:
foo = 5;
std::cout << &foo << std::endl;
std::cout << foo << std::endl;
You'll see that the first result is a series of numbers and letters, whereas the second result is 5.

Now for the array bit. When you pass an array to a function it doesn't take the value of the array, but points to the addresses of the memory cells that the array's data is stored in. So to answer your first question. char** name is pointing to a multidimensional array (double array as you called it), so it's a pointer.

Hope this helped I'm no Bench, but I tried my best

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to MrPickle For This Useful Post:
lwinaung (07-19-2008)
  #3 (permalink)  
Old 07-19-2008, 01:41 AM
lwinaung lwinaung is offline
Novice
Join Date: Jul 2008
Posts: 8
iTrader: (0)
lwinaung is on a distinguished road
Thank You so much for your help. I know about pointer clearly now. But I still confuse in some sentences of ** in program while I am studying. I will write down these later to ask you.

LA

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-19-2008, 04:39 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 116
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
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;
}
the above program should output
Quote:
1
1
Whenever you see anyone mention anything to do with "equivalence" between pointers and arrays, this is what they are talking about. More precisely, they should say that the syntax is equivalent. Arrays and pointers themselves are completely different.


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;
}
The last line of the program may look a little unusual if you're uneasy with pointers - though it has the same effect as my_pointer[3], just using pointer arithmetic, rather than array subscript notation

- 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!

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 06:00 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50