How is *x the same as x[]?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

How is *x the same as x[]?

 
0
  #1
Mar 21st, 2009
I'm unsure of how declaring **x (a pointer to a pointer of x) is the same as declaring x[][] (a two-dimensional array of x's)... The theory I have is this:

int *x = {whatever}; (x is a pointer to the first int value)
*x (first array item, value pointed to by x)
x[2] (goes to the location pointed to by x, plus sizeof(int)*2, resulting in third item in the array)

yes?

--edit--

if my understanding is correct, is this what is referred to as "pointer arithmetic?"
Last edited by winrawr; Mar 21st, 2009 at 11:17 pm.
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 54
Reputation: Intrade is an unknown quantity at this point 
Solved Threads: 5
Intrade Intrade is offline Offline
Junior Poster in Training

Re: How is *x the same as x[]?

 
0
  #2
Mar 22nd, 2009
"Yes."
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How is *x the same as x[]?

 
0
  #3
Mar 22nd, 2009
Originally Posted by Intrade View Post
"Yes."
Ditto. Yes with quotes around the word "yes". The phrasing of this sentence is a bit loose and inaccurate.

I'm unsure of how declaring **x (a pointer to a pointer of x) is the same as declaring x[][] (a two-dimensional array of x's)... The theory I have is this:
**x is a pointer to a pointer to an integer not "a pointer to a pointer of x", and you can have a two-dimensional array of integers, but you can't have a two-dimensional array of x's


x[][] and **x are definitely not the same, but when used to dereference addresses, can accomplish the same thing.

#include<iostream>
using namespace std;


int main ()
{
    int y[4];
    y[0] = 5;
    y[1] = 10;
    y[2] = 15;
    y[3] = 20;
    
    int *x = y;
    int *z = x + 2;
    cout << *z << endl;
    cin.get ();
    return 0;
}

Here's an example, though it is a one-dimensional example, not a two-dimensional example. The line in red is an example of pointer arithmetic. The program will output 15.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Re: How is *x the same as x[]?

 
0
  #4
Mar 22nd, 2009
okay I see,
and yeah I guess the phrasing was a bit loose... but with a char array,
you can define it char x[]="rawrawrawrawrawr"; or char *x="rawrawrawrawrawr"; and have it behave the same, no? Isn't there really no difference between pointers and arrays (since there isn't any bounds checking of arrays...you could just go p[5] even if p is just used as a pointer to some object, couldn't you?) it seems like the [0] is exactly the same as *, except the []s just tell the compiler to add an amount to the address of the pointed-to address in the machine instruction (mov eax,[ebp+whatever])... so what's the difference between **x and x[][]?

--edit--

aside from the fact that int x[5] would allocate the appropriate amount of space to the stack (or heap), where as declaring it int *x cannot have this functionality.
Last edited by winrawr; Mar 22nd, 2009 at 3:33 am.
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 311
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 18
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: How is *x the same as x[]?

 
0
  #5
Mar 22nd, 2009
you can't do one of the following even , if you thinks that way.
That's the difference

  1. char ** chararrayarray =
  2. {
  3. "My first string " ,
  4. "My Second string " ,
  5. "My third string",
  6. "My fourth string "
  7. };
the meaning of this is pointer to pointer to a char , but it can continue until it found the zero character. so this is wrong then.
or

  1. char [][] chararrayarray =
  2. {
  3. "My first string " ,
  4. "My Second string " ,
  5. "My third string",
  6. "My fourth string "
  7. };
the meaning of this is array of ( char array)

but
  1. char *chararrayarray[] =
  2. {
  3. "My first string " ,
  4. "My Second string " ,
  5. "My third string",
  6. "My fourth string "
  7. };
is correct ! That's the difference. The meaning of this is array of (char *) , so you can openly do this.

but you can do this.
  1. char * chararrayarray1[] =
  2. {
  3. "My first string " ,
  4. "My Second string " ,
  5. "My third string",
  6. "My fourth string "
  7. };
  8.  
  9. char ** chararrayarray =chararrayarray1 ;
and try this program .

  1.  
  2. #include <iostream>
  3. using namespace std ;
  4.  
  5. char * chararrayarray1[] =
  6. {
  7. "My first string " ,
  8. "My Second string " ,
  9. "My third string",
  10. "My fourth string "
  11. };
  12.  
  13. char ** chararrayarray = chararrayarray1 ;
  14.  
  15.  
  16. int main(int argc, char* argv[])
  17. {
  18. cout << *(chararrayarray) <<endl ;
  19. cout << *(chararrayarray+1) << endl ;
  20. cout << *(chararrayarray+2) << endl;
  21. return 0;
  22. }
Last edited by NicAx64; Mar 22nd, 2009 at 4:07 am.
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC