Solved

Possibly correct me?

Posted on 2004-10-06
8
224 Views
Last Modified: 2010-04-15
Hi all,

I've just sat a test and got 28/30. However the two I got wrong don't seem just:

===================
What does this do:

int **myVar;
myVar=(int*)malloc(3*sizeof(int*));

Possible answers:
1) Array of 3 integers
2) 3 pointers to integers
3) 3 pointers to pointers
4) pointer to 10 integers

I said 3)

===================
What causes the warning "myFunc must return a value":
myFunc(int arg)
{
     printf("%i",arg);
     return;
}

Possible answers:
1) The assumed return type void is missing
2) The return statement should return a NULL value
3) The return type is assumed to be an integer and that isn't returned
4) Compiler assumes a function with an integer argument must return an integer.

I said 2). However, 1 would also be ok... Since 2, is ok because return type void is then assumed. 1 is ok since then the return is assumed to be NULL...

===================
??????????????
0
Comment
Question by:Unimatrix_001
8 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 130 total points
ID: 12242233

In the first question, the answer is 2.  You are allocating memory

int a;          /*  scalar  */
int *b;        /*  pointer to array of integers  */
int **c;      /*  pointer to array of pointers to integers  */

The progression is 1-for-1 and logical, when you think about it.



In the second statement, 'return' exits the function without placing anything on the stack.  The calling function has no way of knowing whether myFunc() put anything on the stack so it must assume that an integer value is being returned.  However, the return value is whatever happened to be in that location on the stack, possibly from a previous call to another function.

The correct answer is 3.  'int' is assumed if no type is specified.


Sorry you missed these two, but 92% is still worth crowing about.   :)

Kent

0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 120 total points
ID: 12242263
Hi all,

I've just sat a test and got 28/30. However the two I got wrong don't seem just:

===================
What does this do:

int **myVar;
myVar=(int*)malloc(3*sizeof(int*));

Possible answers:
1) Array of 3 integers
2) 3 pointers to integers
3) 3 pointers to pointers
4) pointer to 10 integers

Sigh, the var is declared as being a pointer to a pointer to an integer.

But at execution time it is set to point to an area of memory big enough to hold THREE pointers.
But there's no code to initialize the three pointers.

Answer (1) is not right, as it's not an array.
Answer (2) is not right, as there is only one pointer, the three pointers are not initialized.
Answer (3) is not right as there is no declaration or executable code that makes three pointers.
Answer (4) is not right.

So I'd say there IS no right answer listed.

I said 3)

===================
What causes the warning "myFunc must return a value":
myFunc(int arg)
{
     printf("%i",arg);
     return;
}

Possible answers:
1) The assumed return type void is missing
2) The return statement should return a NULL value
3) The return type is assumed to be an integer and that isn't returned
4) Compiler assumes a function with an integer argument must return an integer.

I said 2). However, 1 would also be ok... Since 2, is ok because return type void is then assumed. 1 is ok since then the return is assumed to be NULL...


Nope, (3) is the right answer.  void is not assumed as the default return type, it's always "int".  
(2) is not too far off, as NULL is very similar to "0", and in fact "return NULL" may even compile okay.
(4) is completely wrong.


===================
??????????????
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 12242350
Ok, thanks guys, points split in favour of Kent.

"Sorry you missed these two, but 92% is still worth crowing about.   :)"
I should have done better still... Thanks anyways. I'll just live and learn. :-)
0
 
LVL 22

Expert Comment

by:grg99
ID: 12247438
>In the first question, the answer is 2.  You are allocating memory

>int a;          /*  scalar  */
>int *b;        /*  pointer to array of integers  */
>int **c;      /*  pointer to array of pointers to integers  */

>The progression is 1-for-1 and logical, when you think about it.


??   How do you get this?

All I see is a pointer to a small block of memory, mistyped to be a **int.

All the sizeof(int*) business gets resolved at compile time and doesnt  have any way to build a chain of pointers.

I'm sorely puzzled by your answer!

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:Sphinx5
ID: 12248141
Hmm...this is going to get interesting pretty quick if there's going to be a bit of debating here! :-)
0
 
LVL 22

Expert Comment

by:grg99
ID: 12248315
Well, we neednt have a heated debate about this.  The question is rather sloppily worded, and C allows all kinds of type-sleight-of-hand;  That int ** CAN be incremented tto point to the other pointesr, and it even CAN be used with subscripts to index into those pointers, even though it isnt declared as an array.   Some of us purists cringe at this, especially in  an academic setting.

So in a VERY loosey-goosey way, in a way no rational user would ever code it, this is "legal" code, IF one eventually fills in the last pointers with pointers to some integers.   Perhaps the question was originally a bit longer, or the writer was trying to obfuscate (something you should only do in a very advanced C class, and maybe never too).

Regards,


grg99

0
 

Expert Comment

by:Sphinx5
ID: 12248379
Sorry if I'm possibly jumping into this question, but just a query. Is something like "int **c" widely used?
0
 
LVL 22

Expert Comment

by:grg99
ID: 12250447

> Is something like "int **c" widely used?

Depends what you mean by "widely".   In the old Macintosh OS they had floating memory blocks which you'd access by using "Handles", which were actually pointers to pointers.  By using this kind of doubly-indirect access the OS could move the memory blocks around as needed.  Very important on a machine with 128K bytes of RAM!    In this common idiomatic use you just KNEW that any variable of type "Handle" was actually a pointer to some arbitrary size block, so it was quite honorable to say
type
   StrArray =  array[1..100] of String;
   StrAHandle = ^^ StrArray ;
var
   HandleToStringArray : StrAHandle;

  Handle := GetBlock( sizeof( StrArray  ) );
 then say  
HandleToStringArray := StrAHandle( Handle );  { type cast }
then use it as:

HandleToStringArray ^^[1] := 'First String';

-----------------------------------------------
Note that this usage was justified here as:
(1) Pascal is very strongly typed.. It's actually difficult to misuse pointers, so you have a lot of help in keeping the info straight.
(2) There's no way to accidentally append an array index onto any of the intermediate pointers.
(3)  It was a very common, well documented, and in fact necessary way of handling memory in those days.
(4) Programmers were rare and relatively inexpensive.

None of those points apply to C usage today.

-----------------
there were similar things you could do when programming in C on the Mac, you just had to be a lot more careful as the C compiler can't help you keep the levels of indirection and subscripting straight for you.

---------------------------------------------------

But in general, just declaring a int **, then without  a lot of comments, pushing a pointer to a block into it, then
pushing another pointer into that, then coercing the last pointer to use as an array base, THAT in MHO is pretty nasty.

Then again, it may be just what you need to ensure your "job security", as it's unlikely your average Joe Coder will be able to figure out what is going on.

Your opinion may vary.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now