77 Replies - 3570 Views - Last Post: 14 June 2009 - 09:18 AM
#1
** used in C++, what is it?
Posted 12 June 2009 - 06:42 PM
I know this is a newbie question, but what is ** when used in c++? like int ** x . can someone tell me what it means! first to answer gets their help thing checked!
Replies To: ** used in C++, what is it?
#2
Re: ** used in C++, what is it?
Posted 12 June 2009 - 06:45 PM
With pointers
pointer to pointer to integer - double indirection
pointer to pointer to integer - double indirection
#3
Re: ** used in C++, what is it?
Posted 12 June 2009 - 06:54 PM
that is double indirection: a pointer to a pointer.
#4
Re: ** used in C++, what is it?
Posted 12 June 2009 - 07:06 PM
it is the address of THE ADDRESS OF x's contents.
#5
Re: ** used in C++, what is it?
Posted 12 June 2009 - 07:11 PM
#7
#8
Re: ** used in C++, what is it?
Posted 12 June 2009 - 08:47 PM
mikeblas, on 12 Jun, 2009 - 07:36 PM, said:
Actually, you're the one who's got it wrong. A pointer to a pointer is the address of a pointer that holds an address. The integer variable doesn't hold an address; it holds the integer.
I called a pointer "integer variable" because that's precisely what a pointer is in C\C++. A low-level language that uses integers to hold integers? Shocking...
int x = 5; int* ptr = &x; cout << ptr << endl; cout << (int)ptr << endl;
You'll get the same exact output (though in different number systems). Why? Because C\C++ interprets pointers and integers as the same thing.
=============
I feel a big shit-storm coming this way...
This post has been edited by Dantheman: 12 June 2009 - 08:54 PM
#10
Re: ** used in C++, what is it?
Posted 12 June 2009 - 08:53 PM
While a pointer MAY be held in an integer this is really not true. Because the size of a pointer may not always match that of an integer -- not only that but a pointer is associated with type. So pointer arithmetic is not the same as pointer arithmetic.
A pointer is a pointer an integer is an integer -- its not really a good idea to mix the two concepts.
A pointer is a pointer an integer is an integer -- its not really a good idea to mix the two concepts.
#11
Re: ** used in C++, what is it?
Posted 12 June 2009 - 08:59 PM
NickDMax, on 12 Jun, 2009 - 07:53 PM, said:
While a pointer MAY be held in an integer this is really not true. Because the size of a pointer may not always match that of an integer -- not only that but a pointer is associated with type. So pointer arithmetic is not the same as pointer arithmetic.
A pointer is a pointer an integer is an integer -- its not really a good idea to mix the two concepts.
A pointer is a pointer an integer is an integer -- its not really a good idea to mix the two concepts.
True. But all the pointer arithmetic and type association is nothing but a compiler feature. You only give a pointer a type so that a compiler can substitute that correct numbers during arithmetic. So an adding 1 to int* is really a 4 being added to an integer.
And I've never seen C\C++ give a different size to pointers and ints. Don't know about other languages, but I was careful to note that I'm only talking about C\C++. Now, if there are some C\C++ implementations that treat those differently, then I will shut up and admit I was wrong.
This post has been edited by Dantheman: 12 June 2009 - 09:02 PM
#12
Re: ** used in C++, what is it?
Posted 12 June 2009 - 09:15 PM
Dantheman, on 12 Jun, 2009 - 07:59 PM, said:
And I've never seen C\C++ give a different size to pointers and ints. Don't know about other languages, but I was careful to note that I'm only talking about C\C++. Now, if there are some C\C++ implementations that treat those differently, then I will shut up and admit I was wrong.
Any compiler that targets Win64 is an example that fits your request.
There are many other implementations where sizeof(int*) is not the same as sizeof(int), particularly in embedded systems. For desktop programming, 64-bit operating systems are probably the most common place you'll find the condition to be true. There's still some debate about which model is best.
Even when sizeof(int*) is the same as sizeof(int), your description of the "int **x" declaration is incorrect. The variable contains an address of the address of the integer, not the address of the integer that has the address.
This post has been edited by mikeblas: 12 June 2009 - 09:17 PM
#13
Re: ** used in C++, what is it?
Posted 12 June 2009 - 09:21 PM
An int ** is a pointer to a pointer to an integer. Probably it exists because someone wants to have an array of integer arrays. Because of pointer-array equivalence in syntax, you point an array of integers with an int *. Then, if you want an array of arrays (a 2D array), you would have a pointer to this array of integers, which translates to a pointer of int *, which translates to int **.
Kanvus, your posts do grate on my nerves because I think they are sloppy and misleading quite often, even if you personally are very knowledgeable. But you have to admit, this statement is extremely puerile. Especially on a technical forum, where the burden is on you to make technically sound and correct arguments.
A pointer is not an integer. "The value representation of pointer types is implementation defined" (3.9.2.3 of the standard).
Just because you've never seen cases where an integer cannot be used to represent a pointer does not mean they are the same. By the way, do you have a 64 bit system around?
Kanvus said:
ur a woman
Dantheman said:
I called a pointer "integer variable" because that's precisely what a pointer is in C\C++.
Just because you've never seen cases where an integer cannot be used to represent a pointer does not mean they are the same. By the way, do you have a 64 bit system around?
#14
Re: ** used in C++, what is it?
Posted 12 June 2009 - 09:28 PM
Generally speaking in C/C++ the size of an integer *should be* the machine word size (though in the end this did not stick). So on an 8bit processor an integer would be 8bit but a pointer is more than likely 16bits.
In 16bit windows/DOS programming the pointer size depended upon the memory model used. While the int size was 16, pointers were either 16 or 32 bits depending upon the memory model.
On the Windows XP64 an int is 32 bits and a pointer is 64 bits.
Pointers and integers are NOT the same, pointers happen to be integral types but they are NOT the same as ints.
In 16bit windows/DOS programming the pointer size depended upon the memory model used. While the int size was 16, pointers were either 16 or 32 bits depending upon the memory model.
On the Windows XP64 an int is 32 bits and a pointer is 64 bits.
Pointers and integers are NOT the same, pointers happen to be integral types but they are NOT the same as ints.
#15
Re: ** used in C++, what is it?
Posted 12 June 2009 - 09:29 PM
Here's the sample code that Dantheman offered:
I modified it slightly to show the size of x and the size of the pointer.
When I built it with a 64-bit compiler, I get warnings (as long as I ask for them):
When I run the code, it's plain to see that the pointer is eight bytes -- 64 bits -- long, while the integer is only four bytes long.
C:\foo>type ptrs.cpp
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int* ptr = &x;
cout << ptr << endl;
cout << (int)ptr << endl;
cout << sizeof(x) << endl;
cout << sizeof(ptr) << endl;
}
I modified it slightly to show the size of x and the size of the pointer.
When I built it with a 64-bit compiler, I get warnings (as long as I ask for them):
C:\foo>cl /EHsc /Wp64 ptrs.cpp Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64 Copyright (C) Microsoft Corporation. All rights reserved. ptrs.cpp ptrs.cpp(11) : warning C4311: 'type cast' : pointer truncation from 'int *' to 'int' Microsoft (R) Incremental Linker Version 8.00.50727.762 Copyright (C) Microsoft Corporation. All rights reserved. /out:ptrs.exe ptrs.obj
When I run the code, it's plain to see that the pointer is eight bytes -- 64 bits -- long, while the integer is only four bytes long.
C:\foo>ptrs 000000000012FEF0 1244912 4 8
|
|

New Topic/Question
Reply




MultiQuote






|