PDA

View Full Version : K&R's strcpy function - problem with pointers



jamadagni
7th January 2006, 13:18
I can understand the coding:

void strcpy(char *s, char *t) {
int i = 0;
while ((s[i] = t[i]) != '\0') i++; }
but if it is changed to:

void strcpy(char *s, char *t) {
while ((*s = *t) != '\0') { s++; t++; } }
then I do not understand it.

In the first implementation, if someone calls strcpy(pname, star); then the function is passed two pointers to the values contained by the variables pname and star (which were previously declared as static char arrays BTW). Since we have told the function by the argument declaration (char *s, char *t) that its input is pointers, the function understands that the actual memory locations are to be updated and this is done by s = t[i].

But in the second implementation what does *s = *t mean? How can first of all *s be used here? Since the declaration is strcopy (char *s, char *t) the variable names s and t refer to the [i]same memory location as pname and star, but then, pname and star were not passed by reference. (No & there.) [In this case, I see that I should be questioning how the first implementation itself worked.] I am getting confused...

Obviously, I am a relative newbie to C and C++. Kindly be patient. Thanks.

munna
7th January 2006, 14:43
s and t are the pointers whereas *s and *t are the values.(Remember * gives you the value).

Therefore,

*s = *t copies the values and initially s and t are pointing to s[0] and t[0] respectively.

s++ and t++ increments the pointer and therefore now *s and *t gives you the values of s[1] and t[1].

Hope its clear
cheers.

jamadagni
7th January 2006, 15:39
s and t are the pointers whereas *s and *t are the values.(Remember * gives you the value).
So when strcpy(a,b) is executed, the values of a and b are passed to the function, and the function matches its argument declaration (char *s, char *t) to (a, b), so s and t are those pointers whose values (extracted by *) are those passed from a and b.

All right, now that's clear, but now how does s[i] = t[i] work in the first example? The pointer is not an array, right? So how can t[i] be a valid expression let alone get assigned to s[i]? If it were *s[i] = *t[i] it would be understandable...

jacek
7th January 2006, 15:57
The pointer is not an array, right?
That's the problem with arrays in C and C++ --- you can treat them as pointers to first element.

In fact x[i] is the same as *(x + i).

Maybe this will shed some light:
http://www.eskimo.com/~scs/cclass/notes/sx10b.html

jamadagni
7th January 2006, 16:36
In fact x[i] is the same as *(x + i).
But only if x is a pointer, right? For example, this will not be true: if char s[]="hello"; then s[3] = *(s + 3) -- correct?

jacek
7th January 2006, 17:08
For example, this will not be true: if char s[]="hello"; then s[3] = *(s + 3) -- correct?
No, s is also a pointer. This inconsistency is a real problem in C and C++.


#include <stdio.h>

int main()
{
char s[] = "hello";

printf( "%c\n", s[3] );
printf( "%c\n", *(s+3) );

return 0;
}

jamadagni
8th January 2006, 02:36
No, s is also a pointer. This inconsistency is a real problem in C and C++.
Why you call this an inconsistency? Just now I read in K&R:

By definition the value of a variable or expression of type array is the address of element zero of the array. Thus after the assignment pa = &a[0]; pa and a have identical values. Since the name of an array is a synonym for the location of the initial element, the assignment pa = &a[0]; can also be written as pa = a;

jacek
8th January 2006, 16:16
Why you call this an inconsistency?
Don't you feel confused when you learn how arrays work in C? Compare arrays from C with those in Pascal or Java.


Just now I read in K&R:
You know, they wouldn't write something like "arrays are more a hack than a real data type".