Hello!
I got one code:
How is possible that compiler lets me use array test1[0] or something even if the test1 is pointer?Code:int main()
{
int *test1,a;
test1=&a;
test1[0]=5;
cout<<test1<<endl;
system("PAUSE");
return 0;
}
Printable View
Hello!
I got one code:
How is possible that compiler lets me use array test1[0] or something even if the test1 is pointer?Code:int main()
{
int *test1,a;
test1=&a;
test1[0]=5;
cout<<test1<<endl;
system("PAUSE");
return 0;
}
Arrays are just a special case of pointers, that's why.
test1[0] is simply another way of writing *(test1 + 0).
Thanks for the reply.
But what if I write
test[1]=5;
and
cout<<*(test1+1)<<endl;
the program crashes. What's the problem?
So if I put a[5], then it would work right?
a is neither a pointer nor an array, but an int.Quote:
Originally Posted by StGuru
Yes, your right. :-)
I got one task where it was test= new int[5];
If you do that, don't forget to delete[] that memory when you're done with it.
Personally I wouldn't use new if there is a low volume of POD or plain old data involved. Just makes life easier.:wave: