View Single Post
Old Jul 12th, 2005, 3:51 AM   #1
silvia
Newbie
 
Join Date: Jul 2005
Posts: 3
Rep Power: 0 silvia is on a distinguished road
Emergency: Confusing C Programming assignment questions

I get these funky programming assignment questions from my course. It is due very soon and the questions are very confusing.. PLEASE HELP!

LINE Contains
-------------------------
50 char * b, q, *r;
200 b=getbuf();
201 q = *b;
212 r= anotherfunction(b);
213-300 /* we want to use ‘q’ and ‘r’ here*/
2000 char * getbuf()
2001 {
2002 char buff[8];
2003-2050 /* unspecified, buff defined here *./
2051 return (char *) buff;
2052 }

1. What will be in variable ‘q’ after line 201 is executed? Under what conditions might this not be so?

2. Is there an alternative, but equivalent, way to write line 2000? If so, what is it?

3. Is getbuf() a reasonable function?

4. Will getbuf() execute at all?

5. Please comment on line 2051.

6. Is getbuf() good practice, and why?

7. What line not given should be provided for compilation?

8. How, exactly, could one get a second ‘char *’ to use back from this function? Be specific in terms of the exact syntax needed. (That is, provide code.) Another way to state this question is how can this function be modified to return a ‘char *’ (that is, it maintains the same return type) 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 (do not use) C++ syntax. Include statements in called and calling functions. Use good programming practice.

MY ANSWERS:
1
The variable q will contain unpredictable data, most likely junk.
I do not quite understand the question “Under what conditions might this not be so?” There’re 2 interpretations to this question.
1 “how to fix the program so that q will not contain junk but the actual array getbuf() returns?”. -- change line 2002 to “static char buff[8]”. So that the array space allocated inside getbuf() will still be available when the control is returned to the caller.
2 “What could happen by chance so that q will not contain junk but the actual array getbuf() returns”?” -- Luckily, when getbuf() ends, the space allocated for the array is not re-used by some other functions.

2
We can write “void * getbuf()”
Or “int * getbuf()” //should have warning for incompatible return type
Or “short * getbuf()” //same warning.
Basically, any pointers will work, since they all have the same size.

3
No it’s not a reasonable function. The function is returning pointer to a local variable, which will disappear when the program returns to the caller.

4
Yes, it will be execute, only with unexpected return result.

5
On line 2051, type casting is not neccessay. Since when we do “char buff[8]”, we have already implicitly made “buff” to be a pointer to point to the first element of the char array.

6
No, it is not a good practice. We should allocate memory in the caller, not inside the function. Because, if we allocate memory inside the function, and if we use static type, each time we call the function, it will reuse the same array and return the same pointer. i.e It will overwrite the information it returned to us last time.

7
I am not sure I understand the question. I assume I am being asked what lines to be added to make the code ready to be compiled.
1) function declaration for getbuf() – char* getbuf();
2) function declaration for anotherfunction() -- char* anotherfunction(char *);
3) void main(){
4) return statement for main
5) implementation of another function.


8
I can just pass in a pointer to char pointer to the function. And change the content of what that char pointer is pointing at.
silvia is offline   Reply With Quote