_ _ _ _ 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 '*', ?

Dani AI

Generated

Short answer: the function must return a value whose type matches the left side of the assignment. Because the call's result is assigned to kptr (an "pointer to int"), the function needs to return an int*. It cannot be void (no value to assign) and returning an int would be the wrong type. This is the point , and were getting at.

Two important C/C++ details to keep in mind. First, when you write an array in a function parameter it is treated as a pointer to the first element (the array "decays" to a pointer) — so int array[] and int *ptr in a parameter list are equivalent; see the array-to-pointer decay note on cppreference. Second, whether returning a pointer is correct depends on object lifetime: returning a pointer to memory that still exists after the call is safe, but returning a pointer to a local automatic variable is undefined behaviour because the object is destroyed at function exit (see storage duration).

Practical advice: if the function wants to hand back part of the caller's array, returning an int* that points into the caller's array is fine. If it allocates memory internally, document who must free it (or better, avoid raw ownership by returning a container). Prefer modern C++ approaches where appropriate — return a std::vector<int> or a std::span<int> (C++20) or accept an output parameter — to avoid dangling-pointer and ownership problems (see cppreference pages for std::vector and std::span).

Also note the minor style point raised: use int main() and return an int (e.g., return 0;) in hosted C++ programs.

Recommended Answers

All 5 Replies

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...

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. :S. Anyways thanks fr the reply.

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...

> 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.

>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:

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.

_ _ _ _ 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 * ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.