Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Dear All, My question is regarding pointer increment. I\'m using Dev-C++Compiler

ID: 3619359 • Letter: D

Question

Dear All, My question is regarding pointer increment. I'm using Dev-C++Compiler, version 4.9.9.2 If we pass an array address to a pointer,we know it holds the starting address of an array. And we canmanipulate the array elements through pointer by pointerincrement. But the problem is when I increment thepointer first time directly in cout statement, it is notincremented, rather it keeps showing the starting address of anarray. For example, I made the following program: main() { int num[10], *ptr; cout<<"starting memory address of anarray = "<<ptr<<endl; //it is showncorrectly cout<<"memory address of next element= "<<ptr++<<endl; /*this doesn't work, rather it keepsshowing the address which was displayed in the first statement i.e,it keeps showing the starting address of an array*/ //however if i write this statement again,then it does show the memory address of next element cout<<ptr++<<endl; //now itwould show the memory address of next element /*if i do not write ptr++ in cout, rather iwrite it explicitly before cout statement, and then displayit, then it does show the memory address ofnext element*/ } Output of the aboveProgram: starting memory address of an array =0x23ffe0 memory address of next element =0x23ffe0 0x23ffe4 As you can see, in second line, it is stilldisplaying the same memory address (which is the starting memoryaddress of an array) despite I wrote ptr++ in cout. However when Iagain wrote ptr++, then it the pointer was incremented Can anyone please tell me why is itso? If i directly write ptr++ in coutstatement, it should show the memory address of nextelement. Thanks in advance Kind Regards, Omer My question is regarding pointer increment. I'm using Dev-C++Compiler, version 4.9.9.2 If we pass an array address to a pointer,we know it holds the starting address of an array. And we canmanipulate the array elements through pointer by pointerincrement. But the problem is when I increment thepointer first time directly in cout statement, it is notincremented, rather it keeps showing the starting address of anarray. For example, I made the following program: main() { int num[10], *ptr; cout<<"starting memory address of anarray = "<<ptr<<endl; //it is showncorrectly cout<<"memory address of next element= "<<ptr++<<endl; /*this doesn't work, rather it keepsshowing the address which was displayed in the first statement i.e,it keeps showing the starting address of an array*/ //however if i write this statement again,then it does show the memory address of next element cout<<ptr++<<endl; //now itwould show the memory address of next element /*if i do not write ptr++ in cout, rather iwrite it explicitly before cout statement, and then displayit, then it does show the memory address ofnext element*/ } Output of the aboveProgram: Output of the aboveProgram: Output of the aboveProgram: starting memory address of an array =0x23ffe0 memory address of next element =0x23ffe0 0x23ffe4 As you can see, in second line, it is stilldisplaying the same memory address (which is the starting memoryaddress of an array) despite I wrote ptr++ in cout. However when Iagain wrote ptr++, then it the pointer was incremented starting memory address of an array =0x23ffe0 memory address of next element =0x23ffe0 0x23ffe4 As you can see, in second line, it is stilldisplaying the same memory address (which is the starting memoryaddress of an array) despite I wrote ptr++ in cout. However when Iagain wrote ptr++, then it the pointer was incremented Can anyone please tell me why is itso? If i directly write ptr++ in coutstatement, it should show the memory address of nextelement. Thanks in advance Kind Regards, Omer

Explanation / Answer

The Problem you are facing is due to the post increment operation in which the value is displayed first and the is incremented like the sample program i given below. ================================================================================= #include<iostream.h> #include<conio.h> void main() {     int x=1;     cout<<" x++ ="<<x++;     cout<<" x++ ="<<x++; } //This program is first displaying the value of x++ =1 (while the output should //be x++ =2 ) the reason is this is a post incrementing operation in which first the value //of x is displayed on the screen  which is at that time 1 and then it is incremented //and store in x that is why when the the second statement run the output of this is //x++ =2; ================================================================================== In your case  main() { int num[10] ; int *ptr=num; cout<<"starting memory address of an array = "<<ptr<<endl;      //it will show the address of first element because it is pointing to first           //element
cout<<"memory address of next element = "<<ptr++<<endl;     //it will also show the first element address because it will first show the                                                                                                                 //the value of the pointer which is still the address of the first element Note                                                                                                              //that still no increment has occour but as the value is displayed then the                                                                                                             //occour and the value of pointer is changed and now it is pointing to the                                                                                                            //second element and that is why when you will print the value of pointer in                                                                                 ^ #include<iostream.h> #include<conio.h> void main() {     int x=1;     cout<<" x++ ="<<x++;     cout<<" x++ ="<<x++; } //This program is first displaying the value of x++ =1 (while the output should //be x++ =2 ) the reason is this is a post incrementing operation in which first the value //of x is displayed on the screen  which is at that time 1 and then it is incremented //and store in x that is why when the the second statement run the output of this is //x++ =2; ================================================================================== In your case  main() { int num[10] ; int *ptr=num; cout<<"starting memory address of an array = "<<ptr<<endl;      //it will show the address of first element because it is pointing to first           //element
cout<<"memory address of next element = "<<ptr++<<endl;     //it will also show the first element address because it will first show the                                                                                                                 //the value of the pointer which is still the address of the first element Note                                                                                                              //that still no increment has occour but as the value is displayed then the                                                                                                             //occour and the value of pointer is changed and now it is pointing to the                                                                                                            //second element and that is why when you will print the value of pointer in                                                                                 ^ main() { int num[10] ; int *ptr=num; cout<<"starting memory address of an array = "<<ptr<<endl;      //it will show the address of first element because it is pointing to first           //element
cout<<"memory address of next element = "<<ptr++<<endl;     //it will also show the first element address because it will first show the                                                                                                                 //the value of the pointer which is still the address of the first element Note                                                                                                              //that still no increment has occour but as the value is displayed then the                                                                                                             //occour and the value of pointer is changed and now it is pointing to the                                                                                                            //second element and that is why when you will print the value of pointer in                                                                                 ^
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote