Quick Question!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 15
Reputation: warpstar is an unknown quantity at this point 
Solved Threads: 0
warpstar warpstar is offline Offline
Newbie Poster

Quick Question!

 
0
  #1
Oct 25th, 2007
  1. _ _ _ _ function2(_ _ _ _ _ _ _ _ _ _ _ );
  2. main{
  3. int n[]= {4, 5, 6};
  4. int *kptr ;
  5. kptr = function2( n ) ;
  6. }

Fill in the blanks and you get:

  1. int *function2( int array[ ]); or
  2. int *function2( int *ptr );
  3. /* parameters names can be omitted */
  4. main{
  5. int n[]= {4, 5, 6};
  6. int *kptr ;
  7. kptr = function2( n ) ;
  8. }

Can you please tell me why its 'int *' in the function prototype, shudnt it be just 'void'? since its not returning anything? Or shudnt it just be 'int' excluding the '*', ?
Last edited by warpstar; Oct 25th, 2007 at 3:06 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Quick Question!

 
0
  #2
Oct 25th, 2007
How do you know it doesn't return anything? All you know (or at least have shown us), are the prototypes. I assume it assigns a value to the int pointer 'kptr' and because its type is an int star assignment from a function must return an int star.

Also, I assume this is an assignment? Hmm. Main returns an integer so 'int main()' with a 'return 0;' at the end of its scope is what you should be having. Was the 'blanked' code given by the teacher...
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 15
Reputation: warpstar is an unknown quantity at this point 
Solved Threads: 0
warpstar warpstar is offline Offline
Newbie Poster

Re: Quick Question!

 
0
  #3
Oct 25th, 2007
Thats a question taken from a sample midterm test, so that is what i assuming all you need to know to answer the question. I still dont really understand why the star is beside int in the function prototype. . Anyways thanks fr the reply.

Originally Posted by twomers View Post
How do you know it doesn't return anything? All you know (or at least have shown us), are the prototypes. I assume it assigns a value to the int pointer 'kptr' and because its type is an int star assignment from a function must return an int star.

Also, I assume this is an assignment? Hmm. Main returns an integer so 'int main()' with a 'return 0;' at the end of its scope is what you should be having. Was the 'blanked' code given by the teacher...
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Quick Question!

 
0
  #4
Oct 25th, 2007
> since its not returning anything?
Well it needs to return something, because there is an assignment here.
kptr = function2( n ) ;

And it needs to return int* (not int), because that's the type of kptr.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: Quick Question!

 
0
  #5
Oct 25th, 2007
>since its not returning anything?
I don't understand. It's clearly returning something because the code that calls the function uses the return value:
  1. kptr = function2( n ) ;
You can't use a function like that unless it returns something, and because kptr is declared as a pointer to int, it's a safe bet that function2 should return a pointer to int. The star means it's a pointer.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Quick Question!

 
0
  #6
Oct 25th, 2007
Originally Posted by warpstar View Post
  1. _ _ _ _ function2(_ _ _ _ _ _ _ _ _ _ _ );
  2. main{
  3. int n[]= {4, 5, 6};
  4. int *kptr ;
  5. kptr = function2( n ) ;
  6. }

Fill in the blanks and you get:

  1. int *function2( int array[ ]); or
  2. int *function2( int *ptr );
  3. /* parameters names can be omitted */
  4. main{
  5. int n[]= {4, 5, 6};
  6. int *kptr ;
  7. kptr = function2( n ) ;
  8. }

Can you please tell me why its 'int *' in the function prototype, shudnt it be just 'void'? since its not returning anything? Or shudnt it just be 'int' excluding the '*', ?
It isn't returning anything? Calling the function is accomplished by
kptr = function2( n ) ;
isn't it? And doesn't it load the value into kptr which is an int *?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC