Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

School of Computing/Technology/Engineering

Module Name : Intro. to Computer Programming


Tutorial :5

1. Write a single statement for each of the indicated tasks.


(a) Declare two floating-point variables number1 and number2, and initialize number1 to 7.3.
Answer : float number 1 , number2;
Number 1 = 7.3;

(b) Declare the variable fPtr to be a pointer to an object of type float.


Answer: float *fPtr;
(c) Assign the address of variable number1 to pointer variable fPtr.
Answer: *fPtr = &number1 ;
(d) Print the value of the object pointed to by fPtr.
Answer: printf(“%.2f”,*fPtr);
(e) Assign the value of the object pointed to by fPtr to variable number2.
Answer : number2 = *fPtr;
(f) Print the value of number2.
Answer: printf(“%.2f”,number2);
(g) Print the address of number1. Use the %p conversion specifier.
Answer:printf(“%p”,&number1);
(h) Print the address stored in fPtr. Use the %p conversion specifier. Is the value printed the same
as the address of number1?
Answer : printf(“%p”,fPtr);
Yes , it’s the same address
(i) Declare a constant pointer cPtr and initialize the pointer with the address of variable
number2. What will be constant?

Answer :Const float*cPtr = &number2;

2. A C program contains the following declaration.


static int x[8] = { 10, 20, 30, 40, 50, 60, 70, 80};
1
(a) What is the meaning of x?
= Answer: x is the name of the array. It represents the starting address of the array x.

(b) What is the meaning of (x + 2)?


= Answer: Represents the address of the third element in the array ‘x’, which is 30
(c) What is the value of *x?
= Answer: ‘*X’ gives the value of the first element in the array , which is 10
(d) What is the value of (*x + 2)?
= Answer: ‘*x’ gives the value of 10 , which means 10 + 2 , which gives us the final value of
12
(e) What is the value of *(x + 2)?
= Answer : This gives use the value of third element of array which is 30 , therefore *(x + 2) is
30

3.
(a) What does this program do?
#include <stdio.h>
void mystery1(char *, const char *);
int main()
{
char string1[80], string2[80];
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
mystery1(string1, string2);
printf("%s\n", string1);
return 0;
}
void mystery1(char *s1, const char *s2)
{
while (*s1 != '\0')
++s1;

2
for ( ; (*s1 = *s2) != '\0'; s1++, s2++)
; /* empty statement */
} This function , ‘*s1 = *s2’ copies the character pointed to by *s2* to the memory
location pointed by ‘s1’.
- After copying , both s1 and s2 are incremented and move to the next character in each string
until it encounters null terminator
- Therefore ‘mystery1’ concatenates ‘string2’ to ‘string1’ . After function is complete , ‘string
1’ will contain the content of ‘string 1’ along with the content of ‘string 2’

4. Find the error in each of the following program segments. If the error can be corrected, explain
how.
(a)
int * x, y;
x = y;
ANSWER:
int *x, y;
y = 10; // Example initialization for y
x = &y; // Assign address of y to x
(b)
float x = 19.34;
float xPtr = &x;
printf("%f\n", xPtr);

ANSWER:
float x = 19.34;
float *xPtr = &x;
printf("%p\n", (void *)xPtr);

(c)
float *realPtr;
long *integerPtr;
integerPtr = realPtr;

3
ANSWER:
float *realPtr;
long *integerPtr;
realPtr = (float *)integerPtr;

char s[ ] = "this is a character array";


int count;
for ( ; *s != '\0' ; s++)
printf("%c ", *s);

Answer:
Remove the int count;
As it doesn’t appear to be any use in the loops or the code itself. So its removed to
avoid any confusion

You might also like