| | |
How is *x the same as x[]?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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?"
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
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
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
Ditto. Yes with quotes around the word "yes". The phrasing of this sentence is a bit loose and inaccurate.
**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.
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.
•
•
•
•
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[][] 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.
okay I see,
and yeah I guess the phrasing was a bit loose... but with a char array,
you can define it
--edit--
aside from the fact that
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
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
you can't do one of the following even , if you thinks that way.
That's the difference
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
the meaning of this is array of ( char array)
but
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.
and try this program .
That's the difference
C++ Syntax (Toggle Plain Text)
char ** chararrayarray = { "My first string " , "My Second string " , "My third string", "My fourth string " };
or
C++ Syntax (Toggle Plain Text)
char [][] chararrayarray = { "My first string " , "My Second string " , "My third string", "My fourth string " };
but
C++ Syntax (Toggle Plain Text)
char *chararrayarray[] = { "My first string " , "My Second string " , "My third string", "My fourth string " };
but you can do this.
C++ Syntax (Toggle Plain Text)
char * chararrayarray1[] = { "My first string " , "My Second string " , "My third string", "My fourth string " }; char ** chararrayarray =chararrayarray1 ;
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std ; char * chararrayarray1[] = { "My first string " , "My Second string " , "My third string", "My fourth string " }; char ** chararrayarray = chararrayarray1 ; int main(int argc, char* argv[]) { cout << *(chararrayarray) <<endl ; cout << *(chararrayarray+1) << endl ; cout << *(chararrayarray+2) << endl; return 0; }
Last edited by NicAx64; Mar 22nd, 2009 at 4:07 am.
Nothing like a kernel pannic !
![]() |
Other Threads in the C++ Forum
- Previous Thread: Numbers in Turbo C++
- Next Thread: Parity Check Matrix
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






