Passing const int * to a function

A

Andrej Prsa

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
 
E

Eric Sosman

Andrej said:
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.

No.
 
E

E. Robert Tisdale

Andrej said:
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
 
A

Andrej Prsa

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


No.

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

Andrej
 
D

Derrick Coetzee

T

Taran

No. For var to be constant the declaration of f should be
int f ( int *const var)

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

Dave Thompson

On 4 Jan 2005 23:59:07 -0800, "Taran" <[email protected]>
wrote:
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
 

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

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,415
Latest member
SharonCran

Latest Threads

Top