pass by value or pass by address?

V

Vols

void f (char *a)
{
a++;
}

int main (void)
{
char a[] = "abc";// or char *a = "abc";
f (a);
puts (a);
return 0;
}

Looks it is 'pass by address'. The result is 'abc' instead of 'bc'. So
actually it is 'pass by value'.
What happened? thanks.

Vol
 
I

Ian Collins

Vols said:
void f (char *a)
{
a++;
}

int main (void)
{
char a[] = "abc";// or char *a = "abc";
f (a);
puts (a);
return 0;
}

Looks it is 'pass by address'. The result is 'abc' instead of 'bc'. So
actually it is 'pass by value'.
What happened? thanks.
It's pass by value. You are passing the value of the pointer a.
 
V

Vols

Then if I want to output 'bc' instead of 'abc', how to write f()?
Thanks.
Vol.
Vols said:
void f (char *a)
{
a++;
}
int main (void)
{
char a[] = "abc";// or char *a = "abc";
f (a);
puts (a);
return 0;
}
Looks it is 'pass by address'. The result is 'abc' instead of 'bc'. So
actually it is 'pass by value'.
What happened? thanks.

It's pass by value. You are passing the value of the pointer a.
 
I

Ian Collins

Vols wrote:

{please don't top-post]
Vols said:
void f (char *a)
{
a++;
}
int main (void)
{
char a[] = "abc";// or char *a = "abc";
f (a);
puts (a);
return 0;
}
Looks it is 'pass by address'. The result is 'abc' instead of 'bc'. So
actually it is 'pass by value'.
What happened? thanks.
It's pass by value. You are passing the value of the pointer a.
Then if I want to output 'bc' instead of 'abc', how to write f()?

[or quote signatures]

void f( char*& a)
{
a++;
}
 

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,266
Messages
2,571,082
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top