Part I Pointers to variables or places RAM (Random Access Memory) are memory add
ID: 3863126 • Letter: P
Question
Part I Pointers to variables or places RAM (Random Access Memory) are memory addresses-the address of the byte of memory to which the pointer is referring (or "referencing"). Some programming languages use pointers to increase efficiency, to make changes to variables indirectly, etc. n C++, if Z is an integer variable, then &Z; is the memory address of that variable. or example if we F have: int 100 cout KK &z; end; The output may look like Ox38ff64. "38ff64" would be the byte address in hexadecimal form. Hexadecimal is just a base 16 integer with 16 digits: 0 through 9 and 'a' through 'f all to represent 0 through 15. Pointer variables in C++ are variables that hold memory addresses. Example int X 1000 line 1 int A 200 line 2 int B 300 im//line 3 int C 100 in line 4 p is a pointer variable line 5 int end //line 6 cout p p++ cout p endl //line 8 This means that p is a pointer originally initialized to hold the memory address of B (10 points) 1. Place these eight lines of code into a new program. What is your output?Explanation / Answer
#include <iostream>
using namespace std;
int main() {
int x=1000;
int a=200;
int b=300;
int c=100;
int *p=&b;
int *q=&a;
int *r=&c;
cout<<"the value of p before increment "<<p<<endl;
p++;
cout<<"the value of p after increment "<<p<<endl;
cout<<"address of a "<<q<<endl;
cout<<"address of c "<<r<<endl;
return 0;
}
Output –
the value of p before increment 0xbff09018
the value of p after increment 0xbff0901c
address of a 0xbff09014
address of c 0xbff0901c
and p now the memory address of C .
------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
int main() {
int A=650;
int *p=&A;
*p=*p-200;
cout<<*p<<endl;
return 0;
}
Output - 450
-----------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
struct PAIR
{
double D;
int I;
};
int main() {
PAIR two;
PAIR *pairptr = &two;
cout<<pairptr->D<<endl;
cout<<pairptr->I<<endl;
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
int main() {
double miles,mpg,g,cost;
cout<<"Enter the number of miles in your trip"<<endl;
cin>>miles;
cout<<"enter the mpg your car gets"<<endl;
cin>>mpg;
cout<<"enter the cost of gasoline"<<endl;
cin>>g;
cost = g*(miles/mpg);
cout<<"your trip will cost : "<<cost<<endl;
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int main() {
int *q ;
q = (int*) malloc(sizeof(int));
*q = 800;
*q = *q - 200;
printf("%d " , *q);
return 0;
}
Output- 600
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.