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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top