Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of pointers

Status
Not open for further replies.

pghsteelers

Technical User
Apr 21, 2006
121
US
What is incorrect about the assignment:

int* arrVar[] = { 2, 5, 94, 03, 2};

I get the error:
error C2440: 'initializing' : cannot convert from 'const int' to 'int *

yet the same format of:

char* arrVar[] = {"howdy",
"goodbye"};

is ok?
 
int* arrVar[] is declaring an array of int* pointers, but you are assigning an array of ints, not pointers. Use this instead:
Code:
int arrVar[]

{"howdy", "goodbye"}; is a 2d array of char* strings, so: char arrVar[] would be wrong in this case since howdy & goodbye have more than just one char.
 
Thank you kindly for the explanation.

However, if I want an array of pointers each point to an int, how do I assign that, so that I have an array of pointers with each element a pointer to a int value.
 
You could do something like this, but I don't know why you'd want to:
Code:
int* arrVar[] = { new int(1), new int(2), new int(3) };
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top