Functions returning pointers

J

JS

If I have a function that looks like this:



char *alloc(int n)
{
....
return allocp - n;

}

I guess the "*" in the first line of the function belongs to "char" and not
the name of the function. "char *"....not "*alloc". Right?

JS
 
I

Ioannis Vranos

JS said:
If I have a function that looks like this:



char *alloc(int n)
{
....
return allocp - n;

}

I guess the "*" in the first line of the function belongs to "char" and not
the name of the function. "char *"....not "*alloc". Right?


Right.
 
A

Alf P. Steinbach

* JS:
If I have a function that looks like this:



char *alloc(int n)
{
....
return allocp - n;

}

I guess the "*" in the first line of the function belongs to "char" and not
the name of the function. "char *"....not "*alloc". Right?

Yes.
 
V

Victor Bazarov

JS said:
If I have a function that looks like this:



char *alloc(int n)
{
....
return allocp - n;

}

I guess the "*" in the first line of the function belongs to "char" and not
the name of the function. "char *"....not "*alloc". Right?

Right. An asterisk cannot be part of a name. It says that the function
'alloc' returns a pointer to char.

V
 
H

HappyHippy

JS said:
If I have a function that looks like this:



char *alloc(int n)
{
....
return allocp - n;

}

I guess the "*" in the first line of the function belongs to "char" and
not
the name of the function. "char *"....not "*alloc". Right?

JS

Yes, you are right, * belongs to the return type (char).
 
J

JS

Victor Bazarov said:
Right. An asterisk cannot be part of a name. It says that the function
'alloc' returns a pointer to char.

V

Just a bit misleading because of the missing blank followed by the "*".
Would seem a bit more clear if one wrote:

char * alloc(int n)
 
K

Karl Heinz Buchegger

JS said:
Just a bit misleading because of the missing blank followed by the "*".
Would seem a bit more clear if one wrote:

char * alloc(int n)

It is the same as
2+3

you can write it any way you want
2+ 3
2 +3
2 + 3

it always means the same thing: the addition of 2 and 3

There is a possible pitfall with that pointer-'*' however:
Actually the situation is this:
The '*' belongs more to the thing on its right then to the thing
on its left. In case of function return types this makes no difference.
But consider:

int* i, j;

What does this define?
Well. It defines a pointer variable named 'i' and (surprise) it defines
an int variable named 'j'. Note: 'j' is an int (!) not a pointer to int (!).
That's why most C programmers prefer to write

int *i, j;

to make this clear without any doubt.

In C++ much less pointers are used, and most programmers agree, that there
should be only one variable defined in each definition, thus a C++ programmer
would write:

int *i;
int j;

and now the whitespace can easily be moved to the data type without fooling
anybody:

int* i;
int j;
 
J

JS

Karl Heinz Buchegger said:
It is the same as
2+3

you can write it any way you want
2+ 3
2 +3
2 + 3

it always means the same thing: the addition of 2 and 3

There is a possible pitfall with that pointer-'*' however:
Actually the situation is this:
The '*' belongs more to the thing on its right then to the thing
on its left. In case of function return types this makes no difference.

but in this function:

char *alloc(int n)

it belongs to the left.....right?
 
I

Ioannis Vranos

JS said:
but in this function:

char *alloc(int n)

it belongs to the left.....right?


Yes. There is no such ambiguity in the language. No identifier (name) is
permitted to contain character '*'.
 
K

Karl Heinz Buchegger

JS said:
but in this function:

char *alloc(int n)

it belongs to the left.....right?

If it is your understanding that alloc() returns a pointer, in particlar
a character pointer, then yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top