468,766 Members | 1,295 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

Passing const int * to a function

Hello, everyone!

When a const int * argument is passed to a function, i.e.

int f (const int *var)
{
printf ("%d\n", *var);
}

int main ()
{
int var = 10;
f (&var);
}

what does that exactly mean? That:

a) the address of var is read-only (var++ is bad),
b) the value of *var is read-only (*var++ is bad),
c) both?

Thanks,

Andrej
Nov 14 '05 #1
7 2529
Andrej Prsa wrote:
Hello, everyone!

When a const int * argument is passed to a function, i.e.

int f (const int *var)
{
printf ("%d\n", *var);
}

int main ()
{
int var = 10;
f (&var);
}

what does that exactly mean? That:

a) the address of var is read-only (var++ is bad),
No.
b) the value of *var is read-only (*var++ is bad),
Almost: The text is correct but the code doesn't
match it. `*var++' is fine, but `(*var)++' is bad.
c) both?


No.

--
Er*********@sun.com

Nov 14 '05 #2
Hi!
When a const int * argument is passed to a function, i.e.

int f (const int *var)
{
printf ("%d\n", *var);
}

int main ()
{
int var = 10;
f (&var);
}

what does that exactly mean? That:

a) the address of var is read-only (var++ is bad),


No.
b) the value of *var is read-only (*var++ is bad),


Almost: The text is correct but the code doesn't
match it. `*var++' is fine, but `(*var)++' is bad.
c) both?


No.


Great, thanks a lot! :) And HAPPY NEW YEAR!

Andrej
Nov 14 '05 #3
<20*********************************@guest.arnes.s i>
Andrej Prsa <an*********@guest.arnes.si> wrote:
int f (const int *var)
{
printf ("%d\n", *var);
} a) the address of var is read-only (var++ is bad),
No. That would be int f( int * const var ).
b) the value of *var is read-only (*var++ is bad),
ITYM (*var)++. *var++ increments var, not *var.
c) both? Thanks, Andrej


--
Christopher Benson-Manica | Vote Republican, elect our Christian
ataru(at)cyberspace.org | Coalition mullahs. Allahu akbar!
Nov 14 '05 #4
Andrej Prsa wrote:
When a const int * argument is passed to a function, i.e.

int f (const int *var) {
printf("%d\n", *var);
}

int main (int argc, char* argv[]) {
int var = 10;
f(&var); return 0; }

what does that exactly mean? That:

a) the address of var is read-only (var++ is bad),
No. That would be

int* const
b) the value of *var is read-only (*var++ is bad),
Yes.
c) both?


No. That would be

const int* const
Nov 14 '05 #5
Andrej Prsa wrote:
When a const int * argument is passed to a function [ . . . ]
what does that exactly mean?


Reading type notation involving pointers can rapidly become an art. See
the comp.lang.c FAQ question 11.9:

http://www.eskimo.com/~scs/C-faq/q11.9.html

Function pointers exacerbate the problem. This is one of the central
syntactic issues addressed by Damian Conway's SPECS syntax for C++ (see
http://www.csse.monash.edu.au/~damia...Proposal.html).

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #6
> Andrej Prsa wrote:
Hello, everyone! When a const int * argument is passed to a function, i.e. int f (const int *var)
{
printf ("%d\n", *var);
} int main ()
{
int var = 10;
f (&var);
} what does that exactly mean? That: a) the address of var is read-only (var++ is bad),
No. For var to be constant the declaration of f should be
int f ( int *const var)
b) the value of *var is read-only (*var++ is bad),
Yes. * var is constant. _and (*var)++ is bad.
Also, *var++ is not incrementing the *var. It's actually incrementing
the var and then dereferencing. ++

precedes over *, see operator precedence.
c) both?

No. For this the declaration of _f would be
int f ( const int *const var)

For * var to be constant
int f ( const int *var)

I would quote my professor who explained reading params to avoid
confusion.
1. Read the variable, in this case _var
2. Bounce first left and then right and then continue in this same
fashion
3. Keep identifiers associated with keywords/variables.

So this would read as
1. var
2. bounce left, * => pointer
3. bounce right, nothing
4. bounce left, const int. Note. identifier const associated with int.

You can continue similarly for _f and get the function.
"f is a function returning int taking params"
param= pointer to constant int as param
so f is
"f is a function returning int taking pointer to constant int as param"

Complete the sentence:
This make "var is a pointer to constant int"

For:
int f ( const int *const var)
1. var
2. bounce left, const
3. bounce right, nothing
4. bounce left, * => pointer
3. bounce right, nothing
4. bounce left, const int. Note. identifier const associated with int.

Complete the sentence:
This make "var is a constant pointer to constant int"

For:
int f ( int *const var)

1. var
2. bounce left, const
3. bounce right, nothing
4. bounce left, * => pointer
3. bounce right, nothing
4. bounce left, int.

Complete the sentence:
This make "var is a constant pointer to int"

HTH.
Regards,
Taran

Nov 14 '05 #7
On 4 Jan 2005 23:59:07 -0800, "Taran" <ta************@honeywell.com>
wrote:
<snip>
I would quote my professor who explained reading params to avoid
confusion.
All declarations, not just params.
1. Read the variable, in this case _var
2. Bounce first left and then right and then continue in this same
fashion
3. Keep identifiers associated with keywords/variables.

Except as overridden by parentheses, should be left for qualifier(s)
only, then right [array] and (function) as far as they go, then left
to * and additional qualifier(s) as far as they go, ending with the
leftmost type specifier(s) and qualifier(s) if any, except that the
storage-class "jumps" right up to the top.

const int volatile static * a [N] is static/internal array of
pointer(s) to const volatile int, though written in poor style.
- David.Thompson1 at worldnet.att.net
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Charles Sullivan | last post: by
reply views Thread by wellingj | last post: by
3 posts views Thread by Giff | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.