- Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
Answer:
(char*(*)( )) (*ptr[N])( );
- main(){
struct student {
char name[30];
struct date dob;
}stud;
struct date{
int day,month,year;
};
scanf("%s%d%d%d",
stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year);
}
Answer:
Compiler Error: Undefined
structure date
Explanation:
Inside the struct definition
of ‘student’ the member of type struct date is given. The compiler doesn’t have
the definition of date structure (forward
reference is not allowed in C in this case) so it issues an error.
- main(){
struct date;
struct student{
char name[30];
struct date dob;
}stud;
struct date{
int day,month,year;
};
scanf("%s%d%d%d",
stud.rollno, &student.dob.day, &student.dob.month,
&student.dob.year);
}
Answer:
Compiler Error: Undefined
structure date
Explanation:
Only declaration of struct
date is available inside the structure definition of ‘student’ but to have a
variable of type struct date the definition of the structure is required.
- There were 10 records stored in “somefile.dat” but the following program printed 11 names. What went wrong?
void main(){
struct student{
char name[30], rollno[6];
}stud;
FILE *fp =
fopen(“somefile.dat”,”r”);
while(!feof(fp)) {
fread(&stud,
sizeof(stud), 1 , fp);
puts(stud.name);
}
}
Explanation:
fread reads 10 records and
prints the names successfully. It will return EOF only when fread tries to read
another record and fails reading EOF (and returning EOF). So it prints the last
record again. After this only the condition feof(fp) becomes false, hence comes
out of the while loop.
- Is there any difference between the two declarations,
int foo(int *arr[]) and
int foo(int *arr[2])
Answer:
No
Explanation:
Functions can only pass
pointers and not arrays. The numbers that are allowed inside the [] is just for
more readability. So there is no difference between the two declarations.
- What is the subtle error in the following code segment?
void fun(int n, int arr[]){
int *p=0;
int i=0;
while(i++<n)
p = &arr[i];
*p = 0;
}
Answer & Explanation:
If the body of the loop
never executes p is assigned no address. So p remains NULL where *p =0 may
result in problem (may rise to runtime error “NULL pointer assignment” and
terminate the program).
- What is wrong with the following code?
int *foo(){
int *s =
malloc(sizeof(int)100);
assert(s != NULL);
return s;
}
Answer & Explanation:
assert macro should be used
for debugging and finding out bugs. The check s != NULL is for error/exception
handling and for that assert shouldn’t be used. A plain if and the
corresponding remedy statement has to be given.
- What is the hidden bug with the following statement?
assert(val++ != 0);
Answer & Explanation:
Assert macro is used for
debugging and removed in release version. In assert, the experssion involves
side-effects. So the behavior of the code becomes different in case of debug
version and the release version thus leading to a subtle bug.
Rule to Remember:
Don’t use expressions that
have side-effects in assert statements.
- void main(){
int *i = 0x400; // i points to the address 400
*i = 0; // set the value of memory location
pointed by i;
}
Answer:
Undefined behavior
Explanation:
The second statement results
in undefined behavior because it points to some location whose value may not be
available for modification. This type of
pointer in which the non-availability of the implementation of the referenced
location is known as 'incomplete type'.
No comments:
Post a Comment