-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_with_array.cpp
More file actions
27 lines (20 loc) · 981 Bytes
/
Copy pathpointer_with_array.cpp
File metadata and controls
27 lines (20 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
int main() {
int a[]={0,3,4,5,6};
int *ptr=a;
//int *ptr=&a; it gives error becuase we can not point to the whole array & name of array is also a pointer which points to the first element of array
//here a==&a are point to same value & *a==a[0] gives same valuse
cout<<a<<endl;
cout<<""<<&ptr<<endl;
cout<<"*ptr gives value at ptr-->"<<*ptr<<endl;
cout<<a[0]<<endl;
//retrieves the element by using pointer
for(int i=0;i<5;i++){
cout<<*(ptr+i)<<"\t";}
cout<<endl;
cout<<"sizeof(a) gives--> "<< sizeof(a)<<endl;//it gives size of whole array
cout<<"sizeof(&ptr) gives--> "<< sizeof(&ptr)<<"\t sizeof(&a) also gives -->"<<sizeof(&a)<<endl;//it gives the size of pointer which point to the first element of array
//it show size of both ptr and &ptr are equal;
cout<<"sizeof(*ptr) gives--> "<<sizeof(*ptr)<<endl; // it give the size of the element or object / a[0]
}