Page 1 of 1
1. Write the following programs then try to understand and write down the output: void main() { int n = 500; int* p = &n
Posted: Fri May 20, 2022 11:58 am
by answerhappygod

- 1 Write The Following Programs Then Try To Understand And Write Down The Output Void Main Int N 500 Int P N 1 (112.56 KiB) Viewed 64 times
1. Write the following programs then try to understand and write down the output: void main() { int n = 500; int* p = &n; cout<<"n: "<<n<<endl; cout<<"*p: "<<*p<<endl; cout<<"p: "<<p<<endl; cout<<"&n: "<<&n<<endl; (*p)++; int j=5; p=&j; cout<<"The pointer now points to y variable and its value is :"<<*p<<endl; *p = 20; cout<<"n value is "<<n<<endl<<"j value is "<<j«<endl; int main() { intalpha = 5; intbeta = 20; int* alphaptr = α int* betaPtr = β cout<< "Manipulating alpha through alphaptr" <<endl; cout<<< "Before: alpha= " << alpha <<endl; cout<< "Adding 5 to value pointed to by alphaptr" <<endl; *alphaPtr += 5; cout<< "After: alpha= " << alpha <<endl; cout<< "\nPrinting beta through betaptr" <<endl; cout<< "Value pointed to by betaptr: " « *betaPtr<<endl; cout<< "Adding 5 to beta" <<endl; beta += 5; cout<< "Value pointed to by betaptr: " << *betaPtr<<endl; return 0; } Pointers and Arrays: Rewrite the following program to use pointers instead of arrays in two ways: (1) Use pointer operators that do not modify the pointer to process the array. You can use the array name as the pointer, or you can define a constant pointer and make to the array (i.e. array's first element).
Pointers and Arrays: Rewrite the following program to use pointers instead of arrays in two ways: (1) Use pointer operators that do not modify the pointer to process the array. You can either use the array name as the pointer, or you can define a constant pointer and make it point to the array (i.e. array's first element). (2) Use pointer operators that modify the pointer to process the array. You should first define a non-constant pointer and make it point to the array. int main() { int a[5]; for (int i=0;i<5;++i) { cout<<"array element: "; cin>>a
; } for (int i=0;i<5; ++i) a=a*10; for(int i=0;i<5; i++) cout<<a<<" "; return 0; } 2/2