Systemnahe Programmierung in C: Felder und Zeichenreihen |
1void
2f (void)
3{
4 char str1[10] = "123456789";
5 char *str2 = "abcdefghi";
6 char *str3;
7
8 str1 = "not OK";
9 str1[5] = 'A';
10 str2[5] = 'B';
11 str2 = "OK";
12 str2[5] = 'C';
13
14 str3 = "OK";
15 *str3 = "not OK";
16
17 str2 = "A";
18 *str2 = 'a';
19 *str2 = "A";
20}
|
1
2int
3strlen1 (char str[])
4{
5 int i = 0;
6
7 while (str[i]) /* (str[i] != 0) */
8 ++i;
9
10 return i;
11}
12
13int
14strlen2 (char str[])
15{
16 int i;
17
18 for (i = 0; str[i]; ++i);
19
20 return i;
21}
22
23int
24strlen3 (char *str)
25{
26 int i;
27
28 for (i = 0; *str++; ++i);
29
30 return i;
31}
32
33int /* <-- problematisch */
34strlen4 (char *str)
35{
36 char *ptr = str;
37
38 while (*ptr++);
39
40 return (ptr - str) - 1; /* <-- problematisch */
41}
|
1void
2strcpy1 (char d[], char s[])
3{
4 int i;
5
6 for (i = 0; s[i]; i++)
7 d[i] = s[i];
8 d[i] = '\0';
9}
10
11void
12strcpy2 (char *d, char *s)
13{
14 while ((*d++ = *s++));
15}
|
1#include <stdlib.h>
2#include <string.h>
3
4char *
5newString (char *s)
6{
7 char *res;
8 res = malloc (strlen (s));
9 if (!res)
10 {
11 exit (1);
12 }
13 return strcpy (res, s);
14}
|
Letzte Änderung: 14.12.2015 | © Prof. Dr. Uwe Schmidt |