But if I create an array of pointers for int data type as follows:
1 2
int var[] = {10, 100, 200, 1000};
int *ptr[] = {var[0], var[1], var[2], var[3]};
I get a compiler error. I understand why I am getting a compilation error in the above method of declaration for array of int data type, as var[i] is not a reference to a variable to which a pointer must point to i.e. its address, but shouldn't I also get error by the same logic in the declaration of my char array of pointer.
What is the reason that its is acceptable in char array of pointers?
Is " a string value " an address of something to which a pointer can point to or is it just a constant string value.
But why isn't the same type of code legal with int* array of pointers.....and I have to type & before every var[i] variable? Is `"great"` type char*, string or something else. Also, pointers don't store values(except addresses) but point to some other variable which has the actual value stored and then we can use by using the dereference operator (*) with the pointer. So, my question is how can we actually store a value in char* ?
> But if I create an array of pointers for int data type as follows...
Normal data types and pointer types are different. They are so different a pointer cannot theorically be converted into an integer, or a double and vice-versa. Except the number (0) - NULL
int var[] = {10, 100, 200, 1000};
int *ptr[] = {var[0], var[1], var[2], var[3]};
You are trying to construct a pointer array using normal integer values from var. As a result, the compiler fails to convert them into pointers and thus an error occured. However, by using typecast, the compiler will compile this line peacefully without triggering a compile error.
I understand the error with the int* example. My problem is with the char* example. My doubt is the data type of a string enclosed in double quotes and also on the difference of the functioning of int* and char* pointers (except the obvious fact that they point to data of different types). I didn't have to use the & operator with the char* example.