defining an array using a pointer

Thread Starter

mentaaal

Joined Oct 17, 2005
451
Hey guys, i noticed that you can do this:

char *test = "hello";

but when i tried to do it for an int array like this:

int *test = {1,2,3,4,5};

It wouldnt work, saying cannot convert from int to int*

Is there a way to do this?
 

Mark44

Joined Nov 26, 2007
628
Hey guys, i noticed that you can do this:

char *test = "hello";

but when i tried to do it for an int array like this:

int *test = {1,2,3,4,5};

It wouldnt work, saying cannot convert from int to int*

Is there a way to do this?
When you declare a pointer variable, you get a block of memory that's large enough to hold the address in memory of something of a specific type.

In your first example, test is a variable that holds the address of a char, namely the character 'h' in the string constant "hello". String constants, unlike other constants, are stored in a part of memory called the string table. You might also remember from other conversations that a string constant has a value--the address in memory of its first character.

Assuming for the moment that the characters in "hello" are stored at the address starting at 0x100, and that each character is stored in a single byte, the characters of this string will be at locations 0x100 through 0x105, which includes the ASCII null (i.e., 0) at the end.

On the other hand, an array of any type other than char isn't stored anywhere, so for the compiler to give you the error message it did, I'm guessing that the compiler evaluated this expression --- {1, 2, 3, 4, 5} -- as 5, by ignoring the braces and interpreting the commas as a series of expressions to be evaluated in sequence.

If that last part isn't clear, try this and see what ends up in x:
Rich (BB code):
int x = 1, 2, 3;
In any case, the compiler was unable to comprehend what you were trying to do, and eventually threw up its hands.

The point of my reply is that when you define a pointer (i.e., allocate storage for the pointer and initialize the storage), you have to put an address of the appropriate type in the pointer variable.

Rich (BB code):
int int_array[5] = {1, 2, 3, 4, 5}; 
int * test = int_array;   // or int * test = &int_array; -- both should work
Hope this helps.
Mark
 

Thread Starter

mentaaal

Joined Oct 17, 2005
451
Hey mark, you should start charging for such lengthy replies!
Yeah i understand what your saying and indeed i understand how to define an array as you said it i was just curious as to why you could do it with a character array and not with an int array.
So this string table business is why, i was just thinking that i was doing something wrong. I guess i am picky about all these things because its nice to see consistancy in a language and frustrating to see exceptions all over the place! This is purely due to my lack of experience but still... if it were more consistant it would be far easier to learn.

Thanks mark!
 

Mark44

Joined Nov 26, 2007
628
Hey mark, you should start charging for such lengthy replies!
Yeah i understand what your saying and indeed i understand how to define an array as you said it i was just curious as to why you could do it with a character array and not with an int array.
So this string table business is why, i was just thinking that i was doing something wrong. I guess i am picky about all these things because its nice to see consistancy in a language and frustrating to see exceptions all over the place! This is purely due to my lack of experience but still... if it were more consistant it would be far easier to learn.

Thanks mark!
Well, the languages (C and C++) are almost consistent. Arrays generally behave as you would expect them to. The lone exception is the char array. Somewhere along the line when C was defined, someone decided that arrays of char should be treated a little differently from all other array types. The same behavior was kept in C++. This special treatment of char arrays was probably for the sake of convenience at the expense of consistency with other array types, inasmuch as char arrays are probably the most common array type (this is the opinion of Kernighan and Ritchie).

Anyway, keep plugging away at it. With more experience I think you'll find that there aren't many other inconsistencies in C and C++.
Mark
 
Top