Department of Electrical Engineering
Embedded Systems (ELEC366)
Tutorial 7: Pointers
For all questions, show all your work in detail
1. Answer the following questions by selecting the correct answer(s) from the choices
provided.
(i) Which of the following gives the value stored at the address pointed to by
pointer a?
a) a* d) *a
b) **a e) a
c) &a*
(ii) Which of the following gives the memory address of integer variable “a”?
a) *a d) a*
b) a e) None
c) &a
(iii) Which of the following gives the memory address of a variable pointed to by
pointer a?
a) &a d) **a
b) a e) None
c) *a
(iv) Observe the following ‘C’ code fragment. To which element will the pointer be
pointing after executing the code?
float A[2][3];
float *i = &A[1][0];
i += 2;
a) A[1][2] d) A[0][3]
b) A[1][3] e) None
c) A[0][2]
(v) Which of the following expressions is equivalent to accessing the 4th element of
an array arr[15]?
a) *(arr+3) a) *arr+4
b) *arr+3 b) None
c) *(arr+4)
(vi) What is the correct prototype of the function called as follows:
X[0]= func(Size, X); having: int X[10], Size;
a) int func(int B, int * A);
b) int func(int A[10], int B);
c) int A func(int A[10], int * B);
d) int func(int * A, int * B);
e) None
2. What will be displayed on the screen by the printf statements in the following code
fragments?
(i) char c = 'a';
char *cptr;
c 0x0028FF47
cptr = &c;
*cptr = 'b';
printf ("%c %p %c \n", c, cptr, *cptr);
(ii) float a, b = 2.5 ;
float *fptr = &b ;
short i = 10 ;
short *iptr = &i ;
a = *fptr * (float) *iptr;
*fptr = a;
printf ("%5.2f \n", *fptr);
3. (a) What will be the elements of matrix m after execution of the
following code fragment? Lines have been numbered to help
justify your answers. Use Figure 1 to show the data memory m[0][0]
content after the code's execution.
1. int m[3][3] = {{2,-1}, {-6}, {4,80}};
2. int *mp = &m[2][2];
3. for(int i = 0; i < 9; i++)
4. {
5. if (*mp > 0) *mp = 1;
6. else *mp = 0;
7. mp --;
8. }
Figure 1
(b) Rewrite the code in question (a) to perform the same task using a pointer to the
entire array.
4. Observe the following code fragment:
if ([Link]() > 0)
{
float num = [Link]();
uint32_t bits;
// insert the statement(s) to reinterpret float as 32-bit integer
//display the result
[Link](bits, HEX);
}
Add the statement(s) to the code to display the 32 bits that make up the floating-point
representation in hexadecimal format.
5. Observe the following program:
void setup()
{
[Link](9600);
while (!Serial);
[Link]("Enter 10 numbers separated by spaces or newlines:");
}
void loop()
{
int A[10], n=0;
// check if user entered something
if ([Link]() > 0)
{
for (int i = 0; i < 10; i++)
A[i]= [Link](); // read next integer
// find the number of non-zero elements
for (int i = 0; i < 10; i++)
if (A[i] != 0) n++;
[Link]("number of samples not equal to zero is: %d\n", n);
[Link]("\nEnter new 10 numbers for another array:");
}
}
Modify the code to use a function that determines the number of non-zero elements in the
array, while keeping the array local to the loop() function and using two different
approaches:
(a) A pointer to the array as the function parameter.
(b) A pointer to the entire array as the function parameter.