我目前正在尝试在c中创建一个程序,它将返回一个指向2个字符串数组的指针。第一个是字符串s在奇数位置的字符,第二个是在偶数位置的字符。我在C中没有经验,所以我需要一些关于这个程序的帮助。我一直在尝试使用python和java知道的代码进行编码,但它似乎没有遵循与指针相同的原则。这里是我的代码:一个c程序,它返回一个指向2个字符串数组的指针
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **parity_strings(const char *s){
char dest[malloc((char)sizeof(s)/2 + 1)][malloc((char)sizeof(s)/2 + 1)]; //trying to allocate memory to an array of size 2 which will hold 2 strings.
int i;
for(i = 0; i < sizeof(s); i+= 2){ //iterating through odd strings
s[0] += dest[i];
}
for(i= 2; i< sizeof(s); i += 2){ //iterating through even strings (I suppose i could have just appended using 1 for loop but oh well
s[1] += dest[i];
}
return dest;
}
int main(int argc, char **argv) {
char **r = parity_strings(argv[1]);
printf("%s %s %s\n", r[0], r[1], argv[1]);
return 0;
}
内存分配也只是一个痛苦...我不知道,如果它正在做我打算做它。我试图将字符串的大小以字节+ 1字节分配给数组Dest的每个索引。
有关如何解决此问题的任何想法?谢谢。