What is difference between char* and int* data type to create array of pointers in C++?

While creating an array of pointers for int data type the following code works:

1
2
int var[] = {10, 100, 200, 1000};
int *ptr[] = {&var[0], &var[1], &var[2], &var[3]};

While creating an array of pointers for char data type the following is legal:

 
char *names[] = {"Mathew Emerson", "Bob Jackson"};

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.
closed account (48T7M4Gy)
char* is a pointer to a char and is useful wrt arrays of char's int* ... array of int's
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
  
  char* name = "great";
  std::cout << name << "!\n";
  
  char* names[] = {"great", "wall", "trump"};
  for(int i = 0; i < 3; i++)
  std::cout << names[i] << '\n';
}
great!
great
wall
trump
Last edited on
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
1
2
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.
Last edited on
It is rather simple when you know it.

1
2
int var[] = {10, 100, 200, 1000};
int *ptr[] = {var[0], var[1], var[2], var[3]};

This is invalid since you try to assign a int to a int*.


1
2
3
4
5
char *names[] = {"Mathew Emerson", "Bob Jackson"};
//equals
char* n1 = "Mathew Emerson";
char* n2 = "Bob Jackson";
char *names[] = {n1, n2};

Valid since "Mathew Emerson" is seen as a char* so you assign char* to char*.
Last edited on
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.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
  const char* name = "great";
  std::cout << name << "!\n";
  
  const char* names[] = {"great", "wall", "trump"};
  for(int i = 0; i < 3; i++)
  std::cout << names[i] << '\n';
  
  int var[] = {10, 100, 200, 1000};
  int *ptr = &var[0];
    
    for(int i = 0; i < 4; i++)
        std::cout << ptr[i] << '\n';  
}
great!
great
wall
trump
10
100
200
1000
Last edited on
Topic archived. No new replies allowed.