yiwll
yiwll

Reputation: 65

Usage of pointers as parameters in the strcpy function. Trying to understand code from book

From my book:

void strcpy (char *s, char *t)
{
int i=0;
while ((s[i] = t[i]) != ’\0’)
++i;
}

I'm trying to understand this snippet of code from my textbook. They give no main function so I'm trying to wrap my head around how the parameters would be used in a call to the function. As I understand it, the "i-number" of characters of string t[ ] are being copied to the string s[ ] until there are no longer characters to read, from the \0 escape sequence. I don't really understand how the parameters would be defined outside of the function. Any help is greatly appreciated. Thank you.

Upvotes: 2

Views: 169

Answers (3)

Chris Taylor
Chris Taylor

Reputation: 53699

Here is how you might use the function (note you should change the function name as it will conflict with the standard library)

void my_strcpy (char *s, char *t)
{
    int i=0; 
    while ((s[i] = t[i]) != ’\0’)
    ++i;
}

int main()
{
    char *dataToCopy = "This is the data to copy";
    char buffer[81];   // This buffer should be at least big enough to hold the data from the 
                       // source string (dataToCopy) plus 1 for the null terminator

    // call your strcpy function
    my_strcpy(buffer, dataToCopy);

    printf("%s", buffer);
}

In the code, the i variable is pointing to the character in the character array. So when i is 0 you are pointing to the first character of s and t. s[i] = t[i]copies the i'th character from t to the i'th character of s. This assignment in C is self an expression and returns the character that was copied, which allows you to compare that to the null terminator 0 ie. (s[i] = t[i]) != ’\0’ which indicates the end of the string, if the copied character is not a null terminator the loop continues otherwise it will end.

Upvotes: 0

Draconis
Draconis

Reputation: 3461

Two things to remember here:

  • Strings in C are arrays of chars
  • Arrays are passed to functions as pointers

So you would call this like so:

char destination[16];
char source[] = "Hello world!";

strcpy(destination, source);
printf("%s", destination);

i is just an internal variable, it has no meaning outside the strcpy function (it's not a parameter or anything). This function copies the entire string t to s, and stops when it sees a \0 character (which marks the end of a string by C convention).

EDIT: Also, strcpy is a standard library function, so weird things might happen if you try to redefine it. Give your copy a new name and all will be well.

Upvotes: 2

Joshua
Joshua

Reputation: 43268

Here's a main for you:

int main()
{
    char buf[30];
     strcpy(buf, "Hi!");
     puts(buf);
     strcpy(buf, "Hello there.");
     puts(buf);
}

The point of s and t are to accept character arrays that exist elsewhere in the program. They are defined elsewhere, at this level usually by the immediate caller or one more caller above. Their meanings are replaced at runtime.

Your get compile problems because your book is wrong. Should read

const strcpy (char *s, const char *t)
{
     ...
     return s;
  }

Where const means will not modify. Because strcpy is a standard function you really do need it to be correct.

Upvotes: 0

Related Questions