User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,575 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,588 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 411 | Replies: 5
Reply
Join Date: Oct 2007
Posts: 15
Reputation: warpstar is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
warpstar warpstar is offline Offline
Newbie Poster

Quick Question!

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

Fill in the blanks and you get:

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

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Quick Question!

  #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  
Join Date: Oct 2007
Posts: 15
Reputation: warpstar is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
warpstar warpstar is offline Offline
Newbie Poster

Re: Quick Question!

  #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  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: Quick Question!

  #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  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: Quick Question!

  #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  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Quick Question!

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

Fill in the blanks and you get:

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

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 *?
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC