Replacing Array Elements by using pointer byreference in C++ -
Replacing Array Elements by using pointer byreference in C++ -
i want replace array element other values reference. facing problem while doing this. code below. in below code getting lastly value 10 after passing reference. want changed value 6,7,8,9,10. please suggest:
#include <iostream> using namespace std; int temp=6; int byreference (int *x){ (int t=0;t<5;t++){ *x=temp+t; } } int main () { int array[5]; (int s=0;s<5;s++){ array[s]=s+1; byreference(&array[s]); cout<<*&array[s]<<endl; } }
without vector:
#include <iostream> using namespace std; int temp=6; int byreference (int *x){ (int t=0;t<5;t++){ *(x+t)=temp+t; } } int main () { int array[5]; (int s=0;s<5;s++){ array[s]=s+1; byreference (array); cout<<array[s]<<endl; } }
output:
6 7 8 9 10
c++
Comments
Post a Comment