Pointers
Difficulty: EasyWhat values are displayed on screen by the following program and why?
int main()
{
int *p1,*p2,*p3;
int n,m,k;
n=21; m=12;
p1=&m; p2=&n;
printf("%d %d %d %d",*p1,*p2,m,n);
p3=p1; p1=p2; p2=p3;
printf("%d %d %d %d",*p1,*p2,m,n);
k=*p1; *p1=*p2; *p2=k;
printf("%d %d %d %d",*p1,*p2,m,n);
return 0;
}
Simple Exercise
Difficulty: EasyWrite an algorithm conversion_minutes_secondes that determines the result of converting a given number of hours (integer type) to minutes and the result of converting the same number of hours to seconds.
Usage with an Array
Difficulty: EasyWrite an algorithm that takes an array as a parameter and computes the min and max of this array (simultaneously).
Segmentation Fault
Difficulty: EasyIn a C file, declare an integer array of size 2 initialized with zeros, and print cell 10. Notice that clang -Wall generates warnings. Ignore them and execute. Observe the possible behaviors at runtime. Same exercise by accessing a cell at index 4200.
Pointer to Pointer
Difficulty: RxWrite a program declaring an integer variable v and initializing it. Declare a pointer pv of the appropriate type for this variable and make it point to v. Declare another pointer ppv of the appropriate type to be able to point to the pointer pv. Make ppv point to pv. Modify the value of v indirectly using the pointer ppv, then verify by printing v.
Logint
Difficulty: HardWrite a procedure logint that takes two integers n and p as parameters, and returns the largest power q of p in n. It also computes the multiplicative coefficient d and the remainder r such that n = d × p^q + r with r < p^q and d < p. r and d will be passed by address. The use of the log function is forbidden. Test using the equalities 27 = 1 × 2⁴ + 11, 98 = 9 × 10¹ + 8.