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.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?
int x = 1, 2, 3;
int int_array[5] = {1, 2, 3, 4, 5};
int * test = int_array; // or int * test = &int_array; -- both should work
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).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!