Hello an question came to my mind about array and pointers. We all know array is nothing just a pointer to it's data type. What if:
1 2 3 4 5 6 7
int array[10];
for(int i=0; i<10; ++i)
{ cout<<array[i]; };
cout<<endl;
int *pointer=NULL;
pointer=&array[0];
It's displaying all the elements of an array. Coming back to question pointer to above array will be array[0]. I agree but what if i opens array in such sense that each element of array is now another variable and having it's own pointer. So we can say an array is a pointer AND a collection of pointers.? Is it possible OR can we have any array of pointers?
@ Peter87: I think his question was based on the false premise of an array being a pointer.
@ OP: An array is a continuous allocation of RAM, it is a container not a pointer. Yes, it is possible for an array to contain pointers and it is possible again for pointers to reference other pointers. Does that help at all?
We all know array is nothing just a pointer to it's data type.
That's not true. An array is not a pointer.
I don't understand your question.
Yeah it's. . . I have read it in a C++ Book by deitel and deitel 8th edition and i am perfect sure array is a pointer to it's data type for example int array[CONSTANT] is a pointer to int. . .
Yeah it's. . . I have read it in a C++ Book by deitel and deitel 8th edition and i am perfect sure array is a pointer to it's data type for example int array[CONSTANT] is a pointer to int. . .
I would invite you to uncomment the commented lines below and then let us know how it worked for you.
@Ahmad1797
Yeah it's. . . I have read it in a C++ Book by deitel and deitel 8th edition and i am perfect sure array is a pointer to it's data type for example int array[CONSTANT] is a pointer to int. .
You are wrong. Consider the example
int a[10];
int *p = a;
std::cout << "sizeof( a ) = " << sizeof( a ) << std::endl;
std::cout << "sizeof( p ) = " << sizeof( p ) << std::endl;
char ** pointers = {
"Don't talk and chew bubblegum at the same time.",
"Don't throw your money away gambling.",
"Listen to your parents."
};
This code is invalid. First of all pointers is not an array. Must be
[code] const char * pointers[] = {
"Don't talk and chew bubblegum at the same time.",
"Don't throw your money away gambling.",
"Listen to your parents."
};
I would invite you to uncomment the commented lines below and then let us know how it worked for you.
#include <iostream>
int main()
{
int array[10] ;
int * pointer ;
std::cout << "array size: " << sizeof(array) << '\n' ;
std::cout << "pointer size: " << sizeof(pointer) << '\n' ;
// ++array; // illegal.
++pointer ;
// array = 0; // illegal.
pointer = 0 ;
}
to answer this read this:
sizeof() : When applied to a reference type, the result is the size of the referenced type
and when applied to array gives the size of whole array in bytes becox it has not been referenced DIRECTLY but pointer does have. You can access every element of array just becox it's first element and it's ADDRESS in which you increment to get next elements.
This code is invalid. First of all pointers is not an array. Must be
[code] constchar * pointers[] = {
"Don't talk and chew bubblegum at the same time.",
"Don't throw your money away gambling.",
"Listen to your parents."
};
if my memory is with me then i remember that char* is basically string. Is am right?
int main()
{
int array[10];
////did some thing and called foo()
foo(&array);
return 0;
}
Why above call is not legal?
The function parameter has type reference to an object of type int. You are passing to it an argument haveing type pointer to an array of 10 elements of type int. So the types of the parameter and of the argument differ.
vlad from moscow (5996)
char * is the type pointer to char.:)
exactly that's what i am saying. it's Pointer to char and string if you ever open <string> library . I have used it in many programs. That's array is a pointer so does string is.
@Ahmad1797
exactly that's what i am saying. it's Pointer to char and string if you ever open <string> library . I have used it in many programs. That's array is a pointer so does string is.
A pointer to character is not an array. Consider
char c = 'A';
char *p = &c;
p is not an array. It refers one object of type char.