Re: Passing const int * to a function
From: Taran (taran.tripathi_at_honeywell.com)
Date: 01/05/05
- Next message: William Gabler: "Re: formula used in calculating diffrence between dates"
- Previous message: Micah Cowan: "Re: Missing initializer"
- In reply to: Derrick Coetzee: "Re: Passing const int * to a function"
- Next in thread: Dave Thompson: "Re: Passing const int * to a function"
- Reply: Dave Thompson: "Re: Passing const int * to a function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 4 Jan 2005 23:59:07 -0800
> 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
- Next message: William Gabler: "Re: formula used in calculating diffrence between dates"
- Previous message: Micah Cowan: "Re: Missing initializer"
- In reply to: Derrick Coetzee: "Re: Passing const int * to a function"
- Next in thread: Dave Thompson: "Re: Passing const int * to a function"
- Reply: Dave Thompson: "Re: Passing const int * to a function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|