| View previous topic :: View next topic |
| Author |
Message |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Sat Aug 16, 2008 2:36 pm Post subject: [C++] Explain something to me |
|
|
What is this?
| Code: | int x = 10;
int *px = &x;
int **ppx; //<- What the hell is that? |
It looks like some kind of pointer, but it has 2 '*'s... And one more thing.
| Code: | Class S{
private:
int x;
public:
S(int temp) : x(temp) {} //<-- What is this also?
}; |
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Aug 16, 2008 2:53 pm Post subject: |
|
|
** means pointer to a pointer. As for the other one, it is a constructor. It is initializing x to whatever value was passed into temp. So if I said:
x would now equal 10.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
pkedpker Master Cheater
Reputation: 1
Joined: 11 Oct 2006 Posts: 412
|
Posted: Sat Aug 16, 2008 3:09 pm Post subject: |
|
|
** is pointer to a pointer which is used mostly for arrays.
** is two dimensional.
since lets say the first * points to 5 int's
the second star would point to 5x5 = 25.
I'd stay away from lvl 2 pointers they could mess with your head.. you gotta then be thinking everything in the first pointer like every int.. has another pointer pointing to it so it doubles on it self.
|
|
| Back to top |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Aug 16, 2008 8:24 pm Post subject: |
|
|
An example of using ** in a way would be like this:
TCHAR *tszStrings[10];
Thats a pointer to 10 strings.
TCHAR tszStrings[10][10];
Thats a pointer to 10 strings, all with 10 characters of space.
Even when you see
int main( int argc, char *argv[] )
argv is a pointer to a array of characters.
_________________
|
|
| Back to top |
|
 |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Sun Aug 17, 2008 2:53 am Post subject: |
|
|
| HornyAZNBoy wrote: | | int **pp = &*p; |
Nope. In a declaration (int **pp), * tells the compiler it is a pointer. In an expression (&*p), * dereferences a pointer (gets the value it points to). & gets the address of the variable (aka a pointer to the variable).
So when you do &*p, you get this: *p is equal to int x, so &(*p) is equal to &(x) is equal to p.
What you want is a pointer to p, so you would do int **pp = &p.
pp = &p = &&x
x = *p = **pp
You can have infinite pointers or circular pointers or whatever you want.
void *a, *b;
(void **) a = &b; //tell compiler to pretend a is a double pointer
(void **) b = &a;
*a is b, **a is a, ***a is b, ****a is a
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
|