C function override

S

sam

I got these two functions and questions from an employer.
I know pointer and pointer array, but how can I implement without modifying
original implementation.
Could someone help me on this question? Does C supports function override?



void func(){
char * p;
p=test();
}
char * test(){
char buf[8];

/*Here*/

return (char *) buf;
}

Question:
How, exactly, could one get a second 'char *' to use back from this
function? Be specific in terms of the exact syntax needed. Another way to
state this question is how can this function be modified to return a 'char *
' from the function, and an additional 'char *' value in one function call.
Please make sure that your answer will work even if the size of the char *
desired is not known in the outside calling function. Avoid C++ syntax.
Include statements in called and calling functions. Use good programming
practice. Although alternatives are acceptable, for this question, please
include an answer which maintains the original return type
 
D

Dan Pop

In said:
sam said:
I got these two functions and questions from an employer.
I know pointer and pointer array, but how can I implement without modifying
original implementation.
Could someone help me on this question? Does C supports function override?



void func(){
char * p;
p=test();
}
char * test(){
char buf[8];

/*Here*/

return (char *) buf;
}

Question:
How, exactly, could one get a second 'char *' to use back from this
function? Be specific in terms of the exact syntax needed. Another way to
state this question is how can this function be modified to return a 'char *
' from the function, and an additional 'char *' value in one function call.

The buf would be automaticaly freeed by system when return from test,
you should use malloc in test instead.

Nope, a mere static is enough. And the cast is downright stupid, of
course.

Also, test() should be defined *before* func(), or a declaration for
test() must be in scope when test() is called from func() (otherwise the
compiler will complain about an int being assigned to a pointer).

Dan
 
A

Arthur J. O'Dwyer

Perhaps as a simple interview question. SmElLs like HoMeWoRk.
^^^
(In _another_ function call? Or in the _same_ function call?
Rhetorical question, unless the OP is actually going to give the
problem a try before posting again. Just noting that the problem
statement is practically ungrammatical as it stands, and certainly
nonsensical.)

Hate to say it, but that's not so easy, since C is nearly a subset of C++,
but it can be done with a bit of elbow grease.

This should do it:
char * foo()
^^^^^^^^^^^^
Isn't this line C++? Would

typedef char *compl;
compl foo()

be a proper fix? :)
^
This line is also C++.
Unfortunately, I don't see any way
to make it not C++, other than simply
combining it with one of the adjacent
lines.
char *new;
new = calloc(1,1);
return new;
}
Although alternatives are acceptable, for this question, please
include an answer which maintains the original return type

Would this do:

typedef struct tag_bar {
int farbarczar[12];
} bar;

char *foo(void)
{
bar foob;
return ((char *) ((int) &foob));
}

Ooh, ooh, I know this one!

"No."

;-)
-Arthur
 
M

Mark A. Odell

^^^^^^^^^^^^
Isn't this line C++?

No. This is a function that takes no args and returns a pointer to char.
I'd define it as char *foo(void) { ... } though. Fully legal C.
 
A

Arthur J. O'Dwyer


% cat test.cxx

char *foo()
{
return 0;
}
% g++ -W -Wall -ansi -pedantic -c test.cxx
%

It is too C++!
This is a function that takes no args and returns a pointer to char.
I'd define it as char *foo(void) { ... } though. Fully legal C.

Well, *of course* it's fully legal C. I was merely pointing out that it's
also legal C++, and Dann was trying to write the function without using
any C++ constructs!

Does that really mean that you didn't see *anything* suspicious about
the suggested "fix"?

typedef char *compl;
compl foo()

;)
-Arthur
 
M

Mark A. Odell

% cat test.cxx

char *foo()
{
return 0;
}
% g++ -W -Wall -ansi -pedantic -c test.cxx
%

It is too C++!

It's legal C++ too, sure. So?
Well, *of course* it's fully legal C. I was merely pointing out that
it's also legal C++, and Dann was trying to write the function without
using any C++ constructs!

I'm not sure that it is possible. I mean, 'char', is a valid C++ keyword
so it depends on what you mean by "construct" I guess.
Does that really mean that you didn't see *anything* suspicious about
the suggested "fix"?

No, since I don't typedef functions I just ignored this.
 

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,013
Latest member
KatriceSwa

Latest Threads

Top