I'm trying to write a program that takes puts even and odd indices of a string into their own arrays (with null-terminators at the end of each), which are stored in one array:
char **result = malloc(sizeof(char*) * 2);
int a, b = 0;
int index = strlen(s) / 2;
if (strlen(s) % 2 == 1) {
result[0] = malloc(sizeof(char) * (index + 2));
result[1] = malloc(sizeof(char) * (index + 1));
result[0][index + 1] = "\0"; // 1
result[1][index] = "\0"; // 2
} else {
result[0] = malloc(sizeof(char) * (index + 1));
result[1] = malloc(sizeof(char) * (index + 1));
result[0][index] = "\0"; // 3
result[1][index] = "\0"; // 4
}
for (int i = 0; i < strlen(s); i++) {
if (i % 2 == 0) {
result[0][a] = s[i];
a++;
} else {
result[1][b] = s[i];
b++;
}
}
return result;
When I compile it, the commented lines get the warning "assignment makes integer from pointer without a cast". I don't understand what's wrong with this code. Help?
int a, b = 0;setsbto zero, but leavesauninitialized. The compiler should be warning you about that, too.