![]() ![]() | strings/string-ptr-3.c - Et program der ved brug af pointere kopierer strengen "Aalborg" ind midt i en anden streng. | Lektion 10 - slide 17 : 51 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 pointers to target and source resp. */ str_1 = str + 2; str_2 = str_aal; /* copy str_all into the middle of str */ while(*str_2 != '\0'){ *str_1 = *str_2; str_1++, str_2++; } /* terminate str */ *str_1 = '\0'; printf("%s\n", str); return 0; }