473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Functions returning pointers

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
Jul 23 '05 #1
9 1181
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
JS wrote:
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.
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3
"JS" <sf****@asdas.com> wrote in message
news:d1**********@news.net.uni-c.dk...
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).
Jul 23 '05 #4
JS wrote:
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
Jul 23 '05 #5
JS

"Victor Bazarov" <v.********@comAcast.net> skrev i en meddelelse
news:mx*******************@newsread1.mlpsca01.us.t o.verio.net...
JS wrote:
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


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)
Jul 23 '05 #6
JS wrote:

"Victor Bazarov" <v.********@comAcast.net> skrev i en meddelelse
news:mx*******************@newsread1.mlpsca01.us.t o.verio.net...
JS wrote:
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


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;

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #7
JS

"Karl Heinz Buchegger" <kb******@gascad.at> skrev i en meddelelse
news:42***************@gascad.at...
JS wrote:

"Victor Bazarov" <v.********@comAcast.net> skrev i en meddelelse
news:mx*******************@newsread1.mlpsca01.us.t o.verio.net...
JS wrote:
> 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


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 in this function:

char *alloc(int n)

it belongs to the left.....right?
Jul 23 '05 #8
JS wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> skrev i en meddelelse
news:42***************@gascad.at...
JS wrote:

"Victor Bazarov" <v.********@comAcast.net> skrev i en meddelelse
news:mx*******************@newsread1.mlpsca01.us.t o.verio.net...
> JS wrote:
> > 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

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 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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #9
JS wrote:
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 '*'.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mackan | last post by:
Hi! I'm trying to declare a structure that will be including pointers to functions. These structures in turn will be pointed to in an array and depending on a index value that specific function...
10
by: Evangelista Sami | last post by:
hello i haven't touch any C code for long time and i dont remember how to declare an array of pointers on functions. i have tried this : ----------------------- 35 void firing_1 36 (firing f,...
5
by: Steve | last post by:
Can anyone tell me if I can have an array of functions that take a variable number of parameters? If it is possible I'd like to know how to declare the array and the functions as its elements. I am...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
1
by: ashutosh | last post by:
hi, i am making the dll for clamav antivirus libraray so that i can make activex control for windows. i complied the library successfully and make the Libclamav.dll file using win32 Api...
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
60
by: jacob navia | last post by:
Gnu C features some interesting extensions, among others compound statements that return a value. For instance: ({ int y = foo(); int z; if (y>0) z = y; else z=-y; z; }) A block enclosed by...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
14
by: Remo D. | last post by:
I know that a variable (void *) is guaranteed to be able to store any pointer and to allow conversion back to the original pointer. I think, then, that I can store a pointer to a function in a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.