Sign in to follow this  
pghTech

Array of Pointers

Recommended Posts

I am having a problem comprehending how array of pointers correct syntax works. I understand the concept and what is occuring with an array of pointers, however I am missing something that I was hoping someone could explain of why I am getting the following error with the following declaration int main() ... ... ... int* arrVar[] = {1, 49, 29, 58, 59}; error: "error C2440: 'initializing' : cannot convert from 'const int' to 'int *" I do know that I can declare the following without the same issue: char* arraVar[] {"howdy", "goodbye"}; Any help is greatly appreciated in understanding how I am approaching declaring an array of points of type int to those values listed above.

Share this post


Link to post
Share on other sites
Quote:
Original post by pghTech
I am having a problem comprehending how array of pointers correct syntax works. I understand the concept and what is occuring with an array of pointers, however I am missing something that I was hoping someone could explain of why I am getting the following error with the following declaration


int main()
...
...
...

int* arrVar[] = {1, 49, 29, 58, 59};

error:
"error C2440: 'initializing' : cannot convert from 'const int' to 'int *"

I do know that I can declare the following without the same issue:

char* arraVar[] {"howdy", "goodbye"};

Any help is greatly appreciated in understanding how I am approaching declaring an array of points of type int to those values listed above.
The elements in the initializer list (the {} brackets) must be of the same type as the elements of the array. "howdy" and "goodbye" are string literals, and are considered character pointers by the compiler; therefore they are of the correct type for the array in question. 1, 49, 29, and so on are of type int, not int*, so they are of a different type than the elements of the array in question, hence the compiler error.

This would be correct:
int arrVar[] = {1, 49, 29, 58, 59};
If you want to initialize the elements of an array of int*, they need to be initialized to null or to actually point to something. For example, this would be valid:
int myInt1, myInt2, myInt3;
int* arrVar[] = { &myInt1, &myInt2, &myInt3 };
The deeper issue here may be the difference between pointers and the objects to which they point, so feel free to ask if you're uncertain on that topic.

(Disclaimer: I'm not a c/c++ pro, so the above may contain inaccuracies.)

Share this post


Link to post
Share on other sites
to simplify the above / reitterate, you have the wrong data types going in to you're pointer array, where you should have addresses you have actual values. change you're elements to pointers, and rid the constant values (they are constant because they are not variable(s) ie. 1,49,26....)

examples -

int intVariable = 6;

int* intPointer = NULL; // NULL = zero or 0;

intPointer = &intVariable; // & this operator returns the address of the variable

// *intPointer == intVariable;

intVariable = *intPointer; // * this operator is used to return the value of the pointer

// 6 == 6;

--------------------------------------------------

cplusplus.com has some good information

*(int*)pointer == value of pointer (type int*)
(int*)pointer == arbitrary (int) address / pointer

--------------------------------------------------

good terms to know

dereference, pointer, address, value

Share this post


Link to post
Share on other sites
Thank you both kindly for that run down, that did help allot.

I guess the confusion comes in, by the fact that the string literals match to the array of pointer type (char*) which to me is char = char* (am I definiting that incorrectly?)

But, using that same thought, int can't be equal to an int*. I guess that is what is really screwing me up. Especially if you can assign a few variables of type int and then plug those variables in as the values of the array elements. Why don't you have to assign pointers of type char to variables of int and then plug them in for the elements value?

Share this post


Link to post
Share on other sites
Your question belongs in For Beginners.

'char' is a *single character*. Actually, it's a single byte. But we're going to forget about Unicode for now and pretend that we can represent letters, punctuation and digits with one byte each with no problems.

'char *' is a pointer to a character. Now here's where it gets tricky. Pointers are *just* numeric values that indicate locations in memory. They have no idea whether the stuff at that location is of the appropriate type (actually, data in memory doesn't have a type; that's just a concept that the compiler has) or whether you're even allowed to touch the stuff there (not all memory necessarily belongs to your program; that's why things like 'new' exist).

By convention, C and C++ code can (C must; C++ usually shouldn't) make use of a char* variable to represent "a string". The way this works is that the byte that is pointed at is the first character in the string, the next byte in memory (in the next numbered location) is the second character, etc. I actually should say zeroth and first characters, respectively, because programmers count from zero ;) Anyway, after all the characters of the real text, there is one more byte, which has a zero value. That's so that C library code (functions like strlen()) can tell where the string data ends.

When you type a double-quoted string in your program, that automatically generates such a "string" in memory, putting in the zero byte for you as well automatically. The text is typically written into a special region of the executable code, and if you set a pointer to it, it will point into the copy of your executable in memory. (Yes, the instructions that make up your program go into the same sort of memory as the variables.)

Now then, an array of X is an array of things, each of which is an X. You make a char*[], and you initialize it with char*'s. Thus {"hello", "world"}. You make an int[], and you initialize it with ints. Thus {123, 456}. You make an int*[], and you initialize it with int*'s. Now, there is no way to type a "literal" that corresponds to a *meaningful* int*: double-quoted strings are a special kind of shortcut. But you could have int variables (possibly global), and then make an array of their addresses: 'int foo = 42; int bar = 23; int* pointers[] = {&foo, &bar};'. Note the ampersands. Applying this "reference operator" to a variable means "insert a pointer to the named variable here". So here we make an array of two pointers: one pointing to the foo variable, and one pointing to the bar variable.

Keep in mind, though, that an int* might also point at an array of integers, of any length. There is *no way*, given just a pointer, to know whether the subsequent blocks of memory are intended to be "part of the pointed-at thing", or even if you're allowed to touch those subsequent blocks of memory. (You can normally assume that the block that's directly pointed at is OK; if that assumption turns out to be wrong, the problem is somewhere *else* in your program.)

And in case you are wondering, you can certainly have an array of char as well: 'char[] hello = {'h', 'e', 'l', 'l', 'o'}'. Notice the *single* quotes. Notice also that you could pass 'hello' (without a subscript) to something that expected a char*, because array names are usable as pointers to the beginning of the array. Notice also that this would blow up if you passed it to a C-style string handling function (e.g. "printf("%s", hello)"), because we didn't put in the zero byte. This is just one of many, MANY reasons you should use std::string for text in C++.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this