Re: Passing const int * to a function

From: Taran (taran.tripathi_at_honeywell.com)
Date: 01/05/05


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



Relevant Pages

  • Re: check to see if value can be an integer instead of string
    ... > have looked at is int, but what is comming out is a string that may ... The "int" builtin function never returns any value but an integer. ... > var = some var passed to my script ...
    (comp.lang.python)
  • Re: template for function pointer
    ... > void multi(double* arrayPtr, int len){ ... > var is defined. ... > incoming array with the double value. ... > is not a function pointer, it will never go into case 2 in the switch ...
    (comp.lang.cpp)
  • Re: Postfix increment
    ... specification doesn't contain a strict evaluation order of arguments. ... Now that we have that we can look at the difference between var++ and ... int varPlusPlus ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: need infomation on const
    ... > I need clearification on using const in these following case. ... > Var x is type int that has value 10 and nobody can change it's value. ... int funct(const int y); ...
    (microsoft.public.vc.language)
  • Re: why this code doesnt work!!
    ... Saksena wrote: ... > typedef T var; ... > int main ...
    (comp.lang.cpp)