I read about pointers and it says that
array names can be used as pointer constants and pointers can be used as array names

and here is an example that prove the first statement (array names can be used as pointer constants)

#include<iostream>
using namespace std;
int main()
{
    int arr[3]={10,20,30};
    for(int i=0;i<3;i++)
    cout<<*(arr+i);
    return 0;
}

Do you have any example that prove the second statement(pointers can be used as array names)?

Recommended Answers

All 5 Replies

Here's a small example:

char* a = "abcdef";
cout << a[2] << endl; //offset 2 of a is c

And here's a good link to describe that:
Click Here

is it possible to declare integer pointer contain 3 values ?

if yes , then how can i declare such pointers ?

The only way I know to make a pointer array with x values is the following.

int * numbers = new int[x];  // where x equals some integer number

why is this not possible to create an integer array using pointer (same as what we do with characters . example char* str="hello world" )

why the statement below is illegal :

int* pointers=10,20,30;
cout<<pointers[1];

but this is legal

char* str="hello world"
cout<<str[1];

please explain about these aspects.
i'm highly consfused with it.

Because 10, 20, 30 does nothing more than evaluate to 30 due to how the comma operator works. 30 is an integer literal, which isn't compatible with a pointer to int unless you use a cast to make it an address:

int *p = reinterpret_cast<int*>(30);

This compiles, but it's not a meaningful address for the pointer.

On the other hand, a string literal internally is an array of const char and therefore compatible with a pointer to char. Note, however, that this compatibility is a historical artifact. To be strictly correct, the type of your pointer should be const char:

const char *str = "hello world";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.