Homwrek 1
Homwrek 1
Assignment 1
int a = 3;
int *b = &a;
cout << b << endl;
cout << *b << endl;
cout << &b << endl;
cout << a << endl;
cout << &a << endl;
NO ERROR
Output:
0113F9CC // Address of a (b is pointing to a)
3 //The value of the pointer b which is pointing to the address of a
0113F9C0 // The address of b
3 //The value of a
0113F9CC // The addres of a
Assignment 2
int x,z;
float y;
char ch, *chp;
int*ip1,*ip2;
float*fp;
x= 100;
y= 20.0;
z= 50;
ch= 'Z';
ip1=&x;
ip2=&z;
fp=&y;
chp=&ch;
ip2= ip1;
ip1=&z;
*ip1=*ip2;
*ip1= 200;
*ip1=*ip2+ 300;
*fp= 1.2;
cout << x << endl;
cout <<y << endl;
cout << z << endl;
cout << ip1 << endl;
cout <<*ip1 << endl;
cout <<&ip1 << endl;
cout << ip2 << endl;
cout <<*ip2 << endl;
cout <<&ip2 << endl;
cout << fp << endl;
cout <<*fp << endl;
cout <<&fp << endl;
cout << chp << endl;
cout <<*chp << endl;
cout <<&chp << endl;
NO ERROR
Output:
100 // value of x
1.2 // value of y
400 // value of z
00CFF784 //Address of z (ip1 is pointing to z)
400 // the value of address that ip1 is pointing to which is z
00CFF754 //Address of ip1
00CFF790 //Address of x (ip2 is pointing to x)
100 // the value of address that ip2 points to which is x
00CFF748 //Address of ip2
00CFF778 //Address of y (fp is pointing to y)
1.2 // the value of address that fp is pointing to which is y
00CFF73C //Address of fp
Z╠╠╠╠╠╠╠╠ÜÖÖ?╠╠╠╠╠╠╠╠É //Address of ch (chp points to ch)
Z //the value of the address that chp is pointing to which is ch
00CFF760 //Address of chp
Assignment 3
int *a = new int; // Allocate memory for pointer a
int *b = new int; // Allocate memory for pointer b
*a = 2;
b = a;
cout << *a << endl;
cout << *b << endl;
delete a; // Delete allocate memory of poiter a and b
delete b; // Delete allocate memory which is already deleted (error)
1 ERROR
Fixed code:
int *a = new int;
int *b = new int;
*a = 2;
b = a;
cout << *a << endl;
cout << *b << endl;
delete a; // Delete allocate memory of pointer a and b
b=NULL; //Clear allocate memory of pointer b
Output:
2 // The value of pointer a
2 // The value of the variable that pointer b is pointing to
Assignment 4
int a = 3;
int *p = &a; //Pointer b is pointing to address of a
cout << *p << endl;
p = new int(5); //Allocate new memory for pointer p with value 5
cout << *p << endl;
NO ERROR
Output:
3 //The value of a which pointer p points to
5 // The value of address which pointer b points to
Assignment 5
int v = 8, *r, *s;
int *p;
int q =100;
p = &q; // p is pointing to q
r = p; // r and p are pointing to q (*r = *p = q = 100)
*p = 20; //(*p = *r = q = 20)
p = new int; //(Allocate new memory address for p)
*r = 30; //(*r = q = 30)
q = v; //(q = v = *r = 8)
s =p; //s and p are pointing to same address
*s = 50; //(*s = *p = 50)
RESULT:
*p = 50
q=8
*r =8
v=8
*s = 50
Assignment 6
int *p , *q , v , nom[5];
p = &v; // p is pointing to v
*p = 12; // *p = v = 12
q = p; // *q = *p = v = 12
nom[0] = *q; // nom[0] =*q= 12
p = nom; // p is pointing to nom[0], *p=12;
p++; // p is pointing to nom[1]
nom[2] = 12;
*p = 13; // *p = nom[1] = 13
*q = 10; // *q = v = 10
v = 11; // *q = v = 11
*(p+3) = 16; // p+3 = &nom[4]. nom[4] = 16
p = &nom[3]; // *p is pointing to nom[3]
*p = 10; // *p = nom[3] = 10
p--; // p = &nom[2]. *p = nom[2] = 12
RESULT:
*p = 12
*q = 11
v = 11
nom[0] = 12
nom[1] = 13
nom[2] = 12
nom[3] = 10
nom[4] = 16
Assignment 7:A
Assignment 8:D
Assignment 9:D
Assignment 10:B
Assignment 11:B
Assignment 12:D
Assignment 13:A
Assignment 14:A
Assignment 15:D
Assignment 16:C
Assignment 17:B
Assignment 18:D
Assignment 19:A
Assignment 20:A
Assignment 21:D
Assignment 22:D
Assignment 23:B
Assignment 24:C
Assignment 25:B
Assignment 26:A
Assignment 27:A
Assignment 28:C
Assignment 29:C
Assignment 30:C