![]() ![]() | strings/string-assignment.c - Tilsvarende forsøg på assignments til de fire tekststrenge. | Lektion 10 - slide 16 : 51 Program 3 |
#include <stdio.h> #include <string.h> int main(void) { char str_1[8], str_2[8], *str_3, str_4[8]; /* Ulovlig brug af initializer */ str_1 = {'A', 'a', 'l', 'b', 'o', 'r', 'g', '\0'}; /* Illegal assignment to array. The array name is constant */ /* Implicite copying of characters do not take place in C. */ str_2 = "Aalborg"; /* Alternative: copy the characters yourself */ strcpy(str_2, "Aalborg"); /* Pointer assignment - OK */ str_3 = "Aalborg"; /* Char by char Assignment - OK, but tedious */ str_4[0] = 'A'; str_4[1] = 'a'; str_4[2] = 'l'; str_4[3] = 'b'; str_4[4] = 'o'; str_4[5] = 'r'; str_4[6] = 'g'; str_4[7] = '\0'; printf("%s\n%s\n%s\n%s\n", str_1, str_2, str_3, str_4); return 0; }