| string-ptr.c - Et program der ved brug af pointere kopierer strengen "Aalborg" ind midt i en anden streng. | Lektion 7 - slide 6 : 26 Program 1 |
#include <stdio.h>
int main(void) {
char str_aal[] = "Aalborg";
char str[14];
char *str_1, *str_2;
int i;
/* fill str with '-' */
for(i = 0; i < 14; i++)
str[i] = '-';
/* let str_1 and str_2 be running pointers */
str_1 = str; str_2 = str_aal;
/* copy str_all into the middle of str */
str_1 += 2;
for( ; *str_2 != '\0'; str_1++, str_2++)
*str_1 = *str_2;
/* terminate str */
*str_1 = '\0';
printf("%s\n", str);
return 0;
}