Array Concept. . . .

Pages: 123
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?
Ahmad1797 wrote:
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.
@ 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?
1
2
3
4
5
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. . .
@ OP: Then why do you have to assign it by reference in Line 7 of your code segment above?
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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 ;  
}


http://ideone.com/4fpdTK

Computergeek01 wrote:
@ OP: Then why do you have to assign it by reference in Line 7 of your code segment above?

There are no references at all in the original post.
Last edited on
@ OP: Then why do you have to assign it by reference in Line 7 of your code segment above?

I am not assigning here. . . I am addressing here. & is performing addressing. i.e. storing address the address of first element of the array.
@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;
Last edited on
Moreover can you give me a link to array of Pointers? that would be helpful in future thanks :)
closed account (S6k9GNh0)
An array of pointers?

1
2
3
4
5
char ** pointers = { 
    "Don't talk and chew bubblegum at the same time.",
    "Don't throw your money away gambling.",
    "Listen to your parents." 
};
cire3866 then can you give me explanation why following is illegal?
[code]
void foo(&array);
[\code]
@computerquip

An array of pointers?

1
2
3
4
5
 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."
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.


LINK :http://en.cppreference.com/w/cpp/language/sizeof
@Ahmad1797

cire3866 then can you give me explanation why following is illegal?
[code]
void foo(&array);
[\code]


I do not know whether the following call is illegal because you did not show the function declaration.:)
1
2
3
4
5
6
7
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." 
 };



if my memory is with me then i remember that char* is basically string. Is am right?
char * is the type pointer to char.:)
I do not know whether the following call is illegal because you did not show the function declaration.:)

It was just a declaration. If so!

Prototype:
void foo(int &);

int main()
{
int array[10];
////did some thing and called foo()
foo(&array);
return 0;
}

Why above call is not legal?

@Ahmad1797

It was just a declaration. If so!

Prototype:
void foo(int &);

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.
Last edited on
1
2
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.
Pages: 123