C aptitude

A

aarklon

Hi all,

I was going through the book "Test your skills in C" by Thamarai selvi
and R murugesan, publisheb by Tata Mcgraw-Hill,
ISBN 0-07-044759-4 , the following are from this book,
now my question is to what extent the following are true..???

1) a function with no action is allowed and is known as dummy function

2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
pointer to a 3 element array of function pointers with a return value
of int and a single parameter of a pointer to char



3)how could stderr be re directed from within a program to force all
error messages to be written to the end of the text file "error.log"
instead of location specified by the operating system

a) stderr = fopen("error.log","w");
b) freopen("error.log","a",stderr);
c) stderr = freopen(stderr,"a","error.log");
d) stderr = fopen("error.log","a");

the answer is given as d option

4) #include<stdlib.h>
#include <stdio.h>
char *format="%d";

void func();

int main(void)
{
int x;
func(scanf,&x);
printf("%d\n",x);

return EXIT_SUCCESS;
}

referring to the sample code given above, which of the following
would be correct implementation
for func ?

a) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,x); }

b) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,&x); }

answer is given as a option
 
S

santosh

Hi all,

I was going through the book "Test your skills in C" by Thamarai selvi
and R murugesan, publisheb by Tata Mcgraw-Hill,
ISBN 0-07-044759-4 , the following are from this book,
now my question is to what extent the following are true..???

1) a function with no action is allowed and is known as dummy function

True. Sometimes they are also called as stub functions.
2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
pointer to a 3 element array of function pointers with a return value
of int and a single parameter of a pointer to char

I think the following should do it:

(int(*)[3](char *))ptr
3)how could stderr be re directed from within a program to force all
error messages to be written to the end of the text file "error.log"
instead of location specified by the operating system

a) stderr = fopen("error.log","w");
b) freopen("error.log","a",stderr);
c) stderr = freopen(stderr,"a","error.log");
d) stderr = fopen("error.log","a");

the answer is given as d option

Wrong. The correct option is (b). The pre-defined streams need not be
lvalues.
4) #include<stdlib.h>
#include <stdio.h>
char *format="%d";

void func();

int main(void)
{
int x;
func(scanf,&x);
printf("%d\n",x);

return EXIT_SUCCESS;
}

referring to the sample code given above, which of the following
would be correct implementation
for func ?

a) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,x); }

b) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,&x); }

answer is given as a option

Yes. That's right. Note though that the call could as well have been:

y(format, x);

or

(****y)(format, x);
 
F

Flash Gordon

Hi all,

I was going through the book "Test your skills in C" by Thamarai selvi
and R murugesan, publisheb by Tata Mcgraw-Hill,
ISBN 0-07-044759-4 , the following are from this book,
now my question is to what extent the following are true..???

1) a function with no action is allowed and is known as dummy function

It is true that it is allowed. It is also true that such functions are
often known (by me at least) as dummy functions. However, whether they
are called dummy functions is nothing to do with C.
2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
pointer to a 3 element array of function pointers with a return value
of int and a single parameter of a pointer to char

I can't think of a good reason for doing such a thing. If there ever was
a good reason I would not do it like that (and would reject anything
like that in a code review) since it can be made far clearer by using a
typedef for the function type (or function pointer type if you prefer).
3)how could stderr be re directed from within a program to force all
error messages to be written to the end of the text file "error.log"
instead of location specified by the operating system

a) stderr = fopen("error.log","w");
b) freopen("error.log","a",stderr);
c) stderr = freopen(stderr,"a","error.log");
d) stderr = fopen("error.log","a");

the answer is given as d option

stderr need not be a modifiable lvalue. I.e. you may not be able to do
the assignment in d. According to the standard the primary use of
freopen is to change the file associated with stdin/out/err for this
very reason! So that leave b as the only possibility, so if the book
really does say d it is wrong.
4) #include<stdlib.h>
#include <stdio.h>
char *format="%d";

void func();

int main(void)
{
int x;
func(scanf,&x);
printf("%d\n",x);

return EXIT_SUCCESS;
}

referring to the sample code given above, which of the following
would be correct implementation
for func ?

a) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,x); }

b) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,&x); }

answer is given as a option

Of the two, a is the only possible valid answer (the correct answer
depends on the version of the C standard in use). However, I don't like
the code anyway :)
 
B

Barry Schwarz

(e-mail address removed) wrote:

snip
2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
pointer to a 3 element array of function pointers with a return value
of int and a single parameter of a pointer to char

I think the following should do it:

(int(*)[3](char *))ptr

Nope. The original is correct.



Remove del for email
 
F

Flash Gordon

Malcolm McLean wrote, On 26/04/08 11:13:
You mean the standard doesn't define anything known as the "dummy"
function. However it is implicit in that

void dummy(void)
{
}

is allowed by C rules of grammar. It wasn't a necessary decision to
allow functions with no code in the body at all.

Yes, and I stated that a function "with no action" is allowed. In fact,
nothing you say above contradicts anything I said and all it adds is an
example.
Dummy functions differ from stub functions. A dummy function does
nothing, a stub function returns valid but trival data. For instance if

<snip>

Not always. However, it has nothing to do with the question.
Both are perfectly valid C programming concepts.

I stated that the one that was asked about was valid. I said nothing
about the other for the same reason I said nothing about FFTs, the
reason being it was not what was being asked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top