J
jrefactors
In the following program, are parameters s in function reverse() and
x in function count() both pass by value? How come value k
is not changed, but value str has changed?
Please advise.
thanks!!
============================================
void reverse(char s[]);
void count(int x);
int main(void)
{
char str[] = "hello";
int k = 10;
printf("before str = %s\n", str);
reverse(str);
printf("after str = %s\n", str);
printf("before k = %d\n", k);
count(k);
printf("after k = %d\n", k);
}
void count(int x)
{ x = x + 5;
}
void reverse(char s[])
{ int c, i, j;
for (i=0, j=strlen(s)-1; i<j; i++, j--)
{ c = s[j];
s[j] = s;
s = c;
}
}
==========================================
Output
==========================================
before str = hello
after str = olleh
before k = 10
after k = 10
x in function count() both pass by value? How come value k
is not changed, but value str has changed?
Please advise.
thanks!!
============================================
void reverse(char s[]);
void count(int x);
int main(void)
{
char str[] = "hello";
int k = 10;
printf("before str = %s\n", str);
reverse(str);
printf("after str = %s\n", str);
printf("before k = %d\n", k);
count(k);
printf("after k = %d\n", k);
}
void count(int x)
{ x = x + 5;
}
void reverse(char s[])
{ int c, i, j;
for (i=0, j=strlen(s)-1; i<j; i++, j--)
{ c = s[j];
s[j] = s;
s = c;
}
}
==========================================
Output
==========================================
before str = hello
after str = olleh
before k = 10
after k = 10