Skip to content

Commit 53da332

Browse files
committed
Program should initilize 'i' before use it
1 parent 345a007 commit 53da332

File tree

1 file changed

+7
-6
lines changed
  • 03-elementary_data_structures/3.06-strings/exercises

1 file changed

+7
-6
lines changed

03-elementary_data_structures/3.06-strings/exercises/ex_3.58.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66

77
char *str_cat(char *s1, char *s2)
88
{
9-
char *res;
10-
int i, j;
9+
char *res, *head;
10+
int i = 0, j;
1111

1212
res = malloc((strlen(s1) + strlen(s2) + 1) * sizeof(char));
13+
head = res;
1314
if (res != NULL)
1415
{
1516
while (s1[i] != '\0')
16-
res[i] = s1[i++];
17+
*res++ = s1[i++];
1718
j = i;
1819
i = 0;
1920
while(s2[i] != '\0')
20-
res[j++] = s2[i++];
21+
*res++ = s2[i++];
2122
res[j] = '\0';
2223
}
23-
return res;
24+
return head;
2425
}
2526

2627
char *str_cpy(char *s)
@@ -30,7 +31,7 @@ char *str_cpy(char *s)
3031
res = malloc((strlen(s) + 1) * sizeof(char));
3132
head = res;
3233
if (res != NULL)
33-
while (*res++ = *s++);
34+
while (*res++ = *s++)
3435
return head;
3536
}
3637

0 commit comments

Comments
 (0)