** used in C++, what is it?

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

77 Replies - 4229 Views - Last Post: 14 June 2009 - 09:18 AM Rate Topic: -----

#1 paser  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 135
  • Joined: 30-May 09

** used in C++, what is it?

Post icon  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!
Is This A Good Question/Topic? 0
  • +

Replies To: ** used in C++, what is it?

#2 jcmaster2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: ** used in C++, what is it?

Posted 12 June 2009 - 06:45 PM

With pointers


pointer to pointer to integer - double indirection
Was This Post Helpful? 1
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: ** used in C++, what is it?

Posted 12 June 2009 - 06:54 PM

that is double indirection: a pointer to a pointer.
Was This Post Helpful? 1
  • +
  • -

#4 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

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.
Was This Post Helpful? 1
  • +
  • -

#5 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: ** used in C++, what is it?

Posted 12 June 2009 - 07:11 PM

View PostKanvus, on 12 Jun, 2009 - 06:06 PM, said:

it is the address of THE ADDRESS OF x's contents.

You think that makes any sense?

It's the address of the integer variable that holds the address of x.
Was This Post Helpful? 0
  • +
  • -

#6 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: ** used in C++, what is it?

Posted 12 June 2009 - 07:26 PM

ur a woman
Was This Post Helpful? 0
  • +
  • -

#7 mikeblas  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 390
  • Joined: 08-February 08

Re: ** used in C++, what is it?

Posted 12 June 2009 - 08:36 PM

View PostDantheman, on 12 Jun, 2009 - 06:11 PM, said:

View PostKanvus, on 12 Jun, 2009 - 06:06 PM, said:

it is the address of THE ADDRESS OF x's contents.

You think that makes any sense?

It's the address of the integer variable that holds the address of x.

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.
Was This Post Helpful? 0
  • +
  • -

#8 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: ** used in C++, what is it?

Posted 12 June 2009 - 08:47 PM

View Postmikeblas, 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

Was This Post Helpful? 0
  • +
  • -

#9 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: ** used in C++, what is it?

Posted 12 June 2009 - 08:50 PM

get me a sammich
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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.
Was This Post Helpful? 0
  • +
  • -

#11 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: ** used in C++, what is it?

Posted 12 June 2009 - 08:59 PM

View PostNickDMax, 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.

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

Was This Post Helpful? 0
  • +
  • -

#12 mikeblas  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 390
  • Joined: 08-February 08

Re: ** used in C++, what is it?

Posted 12 June 2009 - 09:15 PM

View PostDantheman, 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

Was This Post Helpful? 0
  • +
  • -

#13 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

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 said:

ur a woman
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.

Dantheman said:

I called a pointer "integer variable" because that's precisely what a pointer is in C\C++.
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?
Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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.
Was This Post Helpful? 0
  • +
  • -

#15 mikeblas  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 390
  • Joined: 08-February 08

Re: ** used in C++, what is it?

Posted 12 June 2009 - 09:29 PM

Here's the sample code that Dantheman offered:

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


Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »