Professional Documents
Culture Documents
Tutorial 5
Tutorial 5
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;
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