Explain the following statements with underline in the program and write out the
ID: 3661449 • Letter: E
Question
Explain the following statements with underline in the program and write out the result of the program. (hint: using pointers in c++ to ans)
(a)
int x = 30;
int* p = &x; (this statement is underline)
cout << "&x is " << &x << endl; (this statement is underline)
cout << "*p is " << *p << endl; (this statement is underline)
cout << "p= " << p << endl; (this statement is underline)
cout << "&p= " << &p << endl; (this statement is underline)
int y = 10;
p = &y; (this statement is underline)
cout << "&y is " << &y << endl;
cout << "*p is " << *p << endl;
cout << "p= " << p << endl;
cout << "&p= " << &p << endl;
(b)
double x = 3.5;
double* p1 = &x; (this statement is underline)
cout << "&x is " << &x << endl; (this statement is underline)
cout << "*p1 is " << *p1 << endl; (this statement is underline)
cout << "p1= " << p1 << endl; (this statement is underline)
cout << "&p1= " << &p1 << endl; (this statement is underline)
double y = 4.5;
double* p2 = &y;
cout << "&y is " << &y << endl;
cout << "*p2 is " << *p2 << endl;
cout << "p2= " << p2 << endl;
cout << "&p2= " << &p2 << endl;
(c)
string s = "abcd"; (this statement is underline)
string* p = &s; (this statement is underline)
cout << "&s is " << &s << endl; (this statement is underline)
cout << "*p is " << *p << endl; (this statement is underline)
cout << (*p)[0] << endl; (this statement is underline)
cout << "p= " << p << endl; (this statement is underline)
cout << "&p= " << &p << endl; (this statement is underline)
Explanation / Answer
Hi,
Below are the solutions to your questions:
C++ allows us to perform either one of these steps independently on a variable with the & and * operators:
1. &x evaluates to the address of x in memory.
2.*( &x ) takes the address of x and dereferences it – it retrieves the value at that location in memory. *( &x ) thus evaluates to the same thing as x.
Question 1:int x = 30;
int* p = &x; (this statement is underline)
cout << "&x is " << &x << endl; (this statement is underline)
cout << "*p is " << *p << endl; (this statement is underline)
cout << "p= " << p << endl; (this statement is underline)
cout << "&p= " << &p << endl; (this statement is underline)
int y = 10;
p = &y; (this statement is underline)
cout << "&y is " << &y << endl;
cout << "*p is " << *p << endl;
cout << "p= " << p << endl;
cout << "&p= " << &p << endl;
Answer:
1.cout << "&x is " << &x << endl; (this statement is underline)
This statement gives the address of memory location where the variable 'x' is stored.
output:&x is 0xbfebd5c0(say)
2.cout << "*p is " << *p << endl; (this statement is underline)
This statement int *p declares the pointer to an integer value, which we are initializing to the address of x. The value stored in a pointer p can be accessed through the dereferencing operator “*”.
Output:*p is 30
3.cout << "p= " << p << endl; (this statement is underline)
This statement prints the address stored in pointer 'p' variable
output:p= 0xbfc601ac (say)
4.cout << "&p= " << &p << endl; (this statement is underline)
This statement gives the address of pointer p.
Output:&p=13(say)
5.p = &y; (this statement is underline)
Currently the contents of y=10 and &y gives the address of variable y which may be say 0xbfebd5b6 so now statement p=&y assigns the address of varaible y to the variable p
Statements:
cout << "&y is " << &y << endl;
cout << "*p is " << *p << endl;
cout << "p= " << p << endl;
cout << "&p= " << &p << endl;
Ouput:
1.&y is 0xbfebd5b77(say)
2.*p is 10(* dereferences the value of varaible y)
3.p=0xbfebd5b77 (address of y which is 0xbfebd5b77)
4.&p=0xbef77(address of pointer p)
Question 2:
double x = 3.5;
double* p1 = &x; (this statement is underline)
cout << "&x is " << &x << endl; (this statement is underline)
cout << "*p1 is " << *p1 << endl; (this statement is underline)
cout << "p1= " << p1 << endl; (this statement is underline)
cout << "&p1= " << &p1 << endl; (this statement is underline)
double y = 4.5;
double* p2 = &y;
cout << "&y is " << &y << endl;
cout << "*p2 is " << *p2 << endl;
cout << "p2= " << p2 << endl;
cout << "&p2= " << &p2 << endl;
Answer:
1.double* p1 = &x; (this statement is underline)
This statement declares a varaible as a pointer and assignes values to it the address of varaiable x.Suppose the address of x is 0xbfebd5c0 then this statement stores this address.
Output of the statements:
Statement:1.cout << "&x is " << &x << endl; (this statement is underline)
Explaination:This statement gives the address of memeory location where the variable 'x' of type double is stored
output:&x is 0xbfebd5b77(say)
2.cout << "*p1 is " << *p1 << endl; (this statement is underline)
This statement int *p1 declares the pointer to an double value, which we are initializing to the address of x. The value stored in a pointer p can be accessed through the dereferencing operator “*”.
3.cout << "p1= " << p1 << endl; (this statement is underline)
This statement prints the address stored in pointer 'p1' variable
output:0xbfc601ac (say)
4.cout << "&p1= " << &p1 << endl; (this statement is underline)
This statement gives the address of pointer p.
Output:&p1=0x13(say)
Now the statements:
double y = 4.5;
double* p2 = &y;
cout << "&y is " << &y << endl;
cout << "*p2 is " << *p2 << endl;
cout << "p2= " << p2 << endl;
cout << "&p2= " << &p2 << endl;
output:
1.&y is 0xabef77
2.*p2 is 10(* dereferences the value of varaible y)
3.p2=0xbfebd5b77 (address of y which is 0xbfebd5b77)
4.&p2=0xbef77(address of pointer p)
Question 3:
1.string s = "abcd"; (this statement is underline)
A string is a character array that is ‘’ terminated,thus the string s =abcd
string* p = &s; (this statement is underline)
This statement declares a pointer with name p of type string and then assigns the address of the string s to the pointer p.
Statement:
1.cout << "&s is " << &s << endl; (this statement is underline)
Explaination:This statement gives the address of the string s in the memory
Output:&s is 0xadcd555(say)
2.cout << "*p is " << *p << endl; (this statement is underline)
This statement string *p declares the pointer to an string value, which we are initializing to the address of string s. The value stored in a pointer p can be accessed through the dereferencing operator “*”.
Output:*p is abcd
3.cout << (*p)[0] << endl; (this statement is underline)
This statement gives the character 0 of the string s which is 'a' and outputs this value.
Output: a
4.cout << "p= " << p << endl; (this statement is underline)
This statement prints the address stored in pointer 'p' variable
Output: p= 0xbfc601ac (say)
5.cout << "&p= " << &p << endl; (this statement is underline)
This statement
This statement gives the address of pointer p.
Output: &p=0x13(say)
Hope that helps...HAPPY ANSWERING!!!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.