943,552 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 789
  • C++ RSS
Oct 25th, 2007
0

Quick Question!

Expand Post »
C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
warpstar is offline Offline
15 posts
since Oct 2007
Oct 25th, 2007
0

Re: Quick Question!

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...
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 25th, 2007
0

Re: Quick Question!

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.

Click to Expand / Collapse  Quote originally posted by twomers ...
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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
warpstar is offline Offline
15 posts
since Oct 2007
Oct 25th, 2007
0

Re: Quick Question!

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 25th, 2007
0

Re: Quick Question!

>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.
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Oct 25th, 2007
0

Re: Quick Question!

Click to Expand / Collapse  Quote originally posted by warpstar ...
C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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 *?
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++beginner I/0 issue
Next Thread in C++ Forum Timeline: I'm back...More problems, different program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC