Sometimes you'll need to create a copy of a string in an application so that you can manipulate the value without affecting the original. When you work with strings and pointers, you have two ways of copying strings. The first method makes use of the strncpy() function:
strncpy(string1, string2, length);
Once again, the length value should correspond to the available room in string1. This function makes a literal copy of the value of string2 and assigns this to string1.
The strncpy() is fine when you want a second copy of a string, but it does require twice the memory, as that second string must also be stored. If you want to work with a second reference to a string value instead, you can copy a string pointer:
char *string1 = "Sophia"; char *string2; string2 = string1;
The string1 variable is a pointer to the location in memory where the literal string Sophia is stored. The string2 variable is also a pointer to this same location. The end result is two pointers—each of which requires very little memory—that can point to the same string value. This also means that a change to the string value through either pointer will be visible when the string is read through the other pointer, because only one string is really being stored.
In our next example, several words will be read in from the keyboard and stored in memory. The strncpy() function will help make this process possible.
1. | Create a new file or project in your text editor or IDE. |
2. | Type the standard beginning lines of code (Script 11.5): /* copy.c - Script 11.5 */ #include <stdio.h> Script 11.5. This application reads in up to 10 (NUM_STRINGS) words of 10 (STR_LEN) characters in length, copying them each into an array. |
3. | Include the string.h header: #include <string.h> For this application, string.h is required since it defines the strncpy() function. |
4. | Add a function prototype: void discard_input (void); This application will also make use of the discard_input() function, as did the guessing game. |
5. | Set two constant macros: #define NUM_STRINGS 10 #define STR_LEN 10 The words entered by the user will be stored in a multidimensional character array whose parameters are set by these constants. The application will take up to NUM_STRINGS words, each of which can be up to STR_LEN characters long. |
6. | Begin the main function: int main (void) { |
7. | Define two character arrays: char words[NUM_STRINGS][STR_LEN]; char input[STR_LEN]; The first array will store all of the submitted words. The second character array will be used to retrieve the keyed input. |
8. | Define two integers: int i; int count = 0; The i variable is a loop counter. To track the total number of words the user entered—which can be up to 10, the count variable will be incremented. |
9. | Define a for loop: for (i = 0; i < NUM_STRINGS; ++i) { To read in all 10 words, a loop will be used. This iterates from 0 to 1 less than NUM_STRINGS. |
10. | Prompt for and read in a word: printf("Enter a word (or 0 to quit): "); scanf("%9s", input); discard_input(); A word will be read in of up to 9 characters in length (allowing for the tenth available spot to be filled with |