Académique Documents
Professionnel Documents
Culture Documents
1. Suppose that x and y have byte values 0x66 and 0x39, respectively. Fill in the following
table indicating the byte values of the different C expressions:
2. what is the output of the following program, assuming that the address of x is
00367034, the address of a is 00367038 and address of p is 00367058 and address of p2
is 00367060?
#include <stdio.h>
int *p, x;
int a[5] = {100,200,300,400,500};
int *p2;
int main()
{
p=NULL;
x=500;
p=&x;
printf("1) %d %d %p %p %p \n",x,*p,p,&x,&p);
p2=a;
*(p2+1) =*p;
*p= *p2 + *(p2+2);
printf("2) %d %d %d \n",x,*p,*p2);
return 0;
}
3. Find and correct the errors in the following program, if there are any.
5. Fill out the following two box-and-arrow diagrams to describe the state of the program
at Point A and Point B labeled in the code below. Be sure to include the values of any
integers (for a pointer the arrow is sufficient).
int x[3] = {3, 5, 7};
int *p = x;
int *p2p = &p;
/* Point A*/
p++;
(*p2p)++;
(*p)++;
/*Point B*/
Point A
p2p
p
x[3]
x[2]
x[1]
x[0]
Point B
p2p
p
x[3]
x[2]
x[1]
x[0]
6. What will be printed by this code?
void foo()
{
int a=5, b=7, c=-4, d[3] = {4,6,12};
int *p1, *p2;
int x,y,z;
p1 = &d[0];
*(p1++);
(*p1)++;
p2 = &c;
x = a + d[2];
y = *p1 + x;
z = *p2 + b;
*p2 += d[0];
}
7. Given the following code snippet, choose the statement that is valid
struct MyType{
int myInt;
double myDouble;
char myChar;
} hello;
#include <stdio.h>
#include <stdlib.h>
struct car {
char *make;
int row;
int col;
};
typedef struct car Car;
count++;
}
int main ()
{
Car Toyo = {"Toyota", 12};
Car Ford = {"Ford" , 7, 6};
Car Hond = {"Honda" , 8, 5};
Car *cPtr1, *cPtr2, *cPtr3;
cPtr1 = malloc(sizeof(Car));
cPtr2 = &Toyo;
cPtr3 = &Hond;
cPtr1->row = 4;
cPtr1->col = cPtr2->col;
cPtr1->make = Ford.make;
cPtr2 = cPtr1;
printCarPtr (cPtr2);
cPtr3->col = Toyo.row;
printCar (Hond);
cPtr1->make = "Prius";
cPtr1->row = cPtr3->col+2;
cPtr1->col++;
printCarPtr (cPtr1);
free (cPtr1);
printCarPtr (cPtr2);
return 0;
10. Given the following screen output from a Linux shell and assuming you are user:
$ pwd
/home/user/CS2303/q2
$ls -l
total 77
-r-------- 1 user cs2303a14 43 Sep 14 12:42 q1.c