943,569 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 342
  • C++ RSS
Mar 21st, 2009
0

How is *x the same as x[]?

Expand Post »
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.
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
Mar 22nd, 2009
0

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

"Yes."
Reputation Points: 43
Solved Threads: 13
Junior Poster
Intrade is offline Offline
122 posts
since Mar 2009
Mar 22nd, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Intrade ...
"Yes."
Ditto. Yes with quotes around the word "yes". The phrasing of this sentence is a bit loose and inaccurate.

Quote ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Mar 22nd, 2009
0

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

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.
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
Mar 22nd, 2009
0

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

you can't do one of the following even , if you thinks that way.
That's the difference

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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 .

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Numbers in Turbo C++
Next Thread in C++ Forum Timeline: Parity Check Matrix





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC