// A simple example of strings in C
// Illustrates:
// How strcpy does not check the size of the destination
// Getting desired results is somewhat dependent on string sizes
//
// Compile: gcc copystrings.c
// Run: a.out (or a.exe on windows)
// Optional Compile: gcc -o copystrings copystrings.c
// Optional Run: copystrings
#include <stdio.h>
#include <string.h> // Library with strcpy
int main()
{
// Local variables are allocated from high to low addresses
//
char sourceStr[] = "ABCDEFGHIJKLMNOP";
char fillerStr[] = "QRS";
int i = 0;
char destStr[] = "TUV";
printf("String variable sourceStr: '%s'\n", sourceStr);
printf("String variable fillerStr: '%s'\n", fillerStr);
printf("int variable i as dec and str: %d, '%s'\n", i, &i);
printf("String variable destStr: '%s'\n", destStr);
printf("addr: source, filler, dest: %x, %x, %x\n", sourceStr, fillerStr, destStr);
printf("\n");
// No checks of size
strcpy(destStr, sourceStr); // Copying the string
printf("String variable sourceStr: '%s'\n", sourceStr);
printf("String variable fillerStr: '%s'\n", fillerStr);
printf("int variable i as dec and str: %d, '%s'\n", i, &i);
printf("String variable destStr: '%s'\n", destStr);
printf("addr: source, filler, dest: %x, %x, %x\n", sourceStr, fillerStr, destStr);
}
/* Output (when run on rucs):
String variable sourceStr: 'ABCDEFGHIJKLMNOP'
String variable fillerStr: 'QRS'
int variable i as dec and str: 0, ''
String variable destStr: 'TUV'
addr: source, filler, dest: bfe9c04f, bfe9c04b, bfe9c040
String variable sourceStr: 'P'
String variable fillerStr: 'LMNOP'
int variable i as dec and str: 1212630597, 'EFGHIJKLMNOP'
String variable destStr: 'ABCDEFGHIJKLMNOP'
addr: source, filler, dest: bfe9c04f, bfe9c04b, bfe9c040
*/