The Function

double *solve( double a[ ][N], double b[ ])

what does the pointer notation * represent in front of solve?

Dani AI

Generated

, and correctly pointed out that the star in the declaration is part of the return type. The practical questions that matter after that are: who owns the memory the pointer names, and how long does it live? Those are the real pitfalls in day‑to‑day code.

Returning a raw pointer can mean any of these situations: the function hands back a pointer into caller-allocated storage, it returns a pointer to dynamically allocated memory that the caller must free, or (bug) it returns a pointer into a local stack buffer that immediately becomes invalid. Avoid the last case — it creates a dangling pointer.

Safer, clearer options:

  • Return a standard container so ownership and size are explicit (preferred in modern C++). Example approach: return a std::vector<double> and let RVO/move do the rest. See std::vector on cppreference.
  • If you must return a heap buffer, return an owning smart pointer such as std::unique_ptr<T[]> to make ownership transfer explicit. See std::unique_ptr on cppreference.
  • For non-owning views, accept or return a span-like type (C++20 std::span) so callers know the view size and that no ownership changes. See std::span on cppreference.
  • Or require the caller to provide the output buffer (preferred in low-level APIs) so allocation strategy is explicit.

Also note that array syntax in parameter lists decays to pointer types, so any size information must be conveyed separately (template parameters, explicit size argument, or container types). Pick the style that makes ownership and lifetime unambiguous for future readers and maintainers.

Recommended Answers

All 3 Replies

It means the function is going to return a pointer to a double variable or possibly a pointer to an array of doubles. The * means a pointer.

Read it as if the * sticks to the type name, not the function name.

Pointer syntax in C/C++ is very flexible, but likewise very confusing. You can say something like int i, *pi, **ppi; but this should be read carefully as

int i;
int* pi;
int** ppi;

:an integer, a pointer to an integer, and a pointer to a pointer to an integer. Yeah, I know... read it again anyway... (I attached the "*" to the int because it is part of the type of each variable, but it could be anywhere between the base type and the variable name.)

In C, you are forgiven a lot when dealing with the type of things. Hence, as already mentioned, the variable int* pi can be a pointer to a single integer or a pointer to a whole lot of them. Typically, programmers will state their intent by saying either int *a --pointer to a single int, or int a[] --pointer to an array of ints.
However, you can't use the [] brackets when declaring a function return type, so you are stuck with the *.

Hope this helps.

commented: pretty decent breakdown +1
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.