嗨我有一个小功能,从句子中删除单词,以2个辅音开头。有功能:
char * fun_zod(char * sak)
{
char * sep = " "; //Zodziu atskirejas - tarpas
char * zodis = strtok(sak,sep); //Nurodome kad sakini suskirstytume i zodzius po viena
char * zodmas[20];
int i = 0;
zodmas[0] = zodis; //Iraso atskirai kiekviena zodi i masyva
while (zodis != NULL)
{
if (zodis == NULL)
break;
i++;
zodis = strtok(NULL,sep);
zodmas[i] = zodis;
}
int n=i;
for(int j = 0;j < n;j++)
{
if (!( zodmas[j][0] == 'a' || zodmas[j][0] == 'e' || zodmas[j][0] == 'i' || zodmas[j][0] == 'o' || zodmas[j][0] == 'u' ) && !( zodmas[j][1] == 'a' || zodmas[j][1] == 'e' || zodmas[j][1] == 'i' || zodmas[j][1] == 'o' || zodmas[j][1] == 'u' ))
{
zodmas[j]="";
}
}
for (i=0;i < n; i++)
{
printf("%s ", zodmas[i]);
}
}
现在我想将结果返回到main函数,打印结果不是来自函数,而是来自main函数。我知道在C中我不能从函数返回数组。也许有人可以建议我如何将结果返回主函数?谢谢你的帮助
答案 0 :(得分:0)
将zodmas和n作为参数传递。
为非溢出zodmas添加测试
char * fun_zod(char * sak, int *n, char **zodmas)
{
char * sep = " "; //Zodziu atskirejas - tarpas
char * zodis = strtok(sak,sep); //Nurodome kad sakini suskirstytume i zodzius po viena
int i = 0;
zodmas[0] = zodis; //Iraso atskirai kiekviena zodi i masyva
while (zodis != NULL && i < *n)
{
if (zodis == NULL)
break;
i++;
zodis = strtok(NULL,sep);
zodmas[i] = zodis;
}
*n=i;
for(int j = 0;j < i;j++)
{
if (!( zodmas[j][0] == 'a' || zodmas[j][0] == 'e' || zodmas[j][0] == 'i' || zodmas[j][0] == 'o' || zodmas[j][0] == 'u' ) && !( zodmas[j][1] == 'a' || zodmas[j][1] == 'e' || zodmas[j][1] == 'i' || zodmas[j][1] == 'o' || zodmas[j][1] == 'u' ))
{
zodmas[j]="";
}
}
}
int main(void) {
int n = 20;
char *zodmas[20];
fun_zod(strdup("toto tutu tata"), &n, zodmas);
for (int i=0;i < n; i++)
{
printf("%s ", zodmas[i]);
}
return 0;
}
答案 1 :(得分:0)
我不建议这样做,但您可以将数组打包到结构中并返回该结构:
struct a {
char *x[30];
};
struct a foo()
{
struct a b = {};
return b;
}
int
main (int argc, char *argv[])
{
struct a f;
f = foo();
return 0;
}
您可以返回指向数组开头的指针
char **
foo()
{
char **array;
array = calloc(30, sizeof *array);
return array;
}
可以通过各种方式传达阵列的长度:
NULL
,调用函数可以通过检查NULL
来确定数组的长度。长度可以通过另一个变量返回:
char **
foo (int *n)
{
char **array;
*n = random_length_of_the_array
array = calloc(30, sizeof *array);
return array;
}
int
main (int argc, char * argv[])
{
char **a;
int length;
a = foo (&length);
...
free (a);
...
您可以负责将数组分配给调用函数。例如,strncpy
和memcpy
以这种方式工作。
答案 2 :(得分:0)
您可以将数组指针返回到已动态分配的内存,但调用者负责释放该内存。我使用了一个字符串指针数组,可以是任何长度(受内存限制)。知识的数量是已知的,因为我使用了NULL
结束标记。因此,数组中始终至少有一个指针 - 空列表只是一个元素值NULL
。我本来可以使用链表来完成这项工作 - 也许更优雅 - 但问题是如何从函数返回数组?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isvowel(int ch)
{
char *vowels = "AEIOUaeiou";
return (strchr(vowels, ch) != NULL);
}
void free_zod(char **zod)
{
int i = 0;
while (zod[i] != NULL)
free (zod[i++]);
free (zod);
}
char **fun_zod(char * sak)
{
char *sep = " \r\n\t"; // weed out tabs and newlines too
char *zodis;
char **zodmas = NULL;
int i = 0, len;
zodmas = malloc(sizeof(char*));
zodmas[0] = NULL; // end marker
zodis = strtok(sak,sep);
while (zodis != NULL)
{
len = strlen(zodis);
if (!isvowel(zodis[0]) && !isvowel(zodis[1]) && strlen(zodis)>=2)
{
zodis = "";
len = 0;
}
zodmas = realloc(zodmas, (i+2) * sizeof(char*));
zodmas[i] = malloc(len+1); // string space
strcpy(zodmas[i], zodis);
zodmas[++i] = NULL; // end marker
zodis = strtok(NULL,sep); // next token
}
return zodmas;
}
int main (void)
{
char **zod;
char input[201];
int i = 0;
if (fgets(input, 200, stdin) == NULL)
return 0;
zod = fun_zod(input); // get array pointer
while (zod[i] != NULL) // NULL is ends marker
printf("%s\n", zod[i++]);
free_zod(zod);
return 0;
}
节目输出
one two three four five six seven eight nine ten eleven twelve
one
four
five
six
seven
eight
nine
ten
eleven
答案 3 :(得分:-1)
将数组从main()函数传递给fun_zod()函数并使用它。 不要在fun_zod中使用数组,那么在那种情况下数组将被清除,因为临时变量概念。
示例:
void func(int *a)
{
/* use this array here , and u dont need to send it back, it will be updated in main func also. */
/* change array value here */
a[0] = 7;
}
void main()
{
int arr[10];
func(arr);
/* change will reflect here*/
}
答案 4 :(得分:-1)
#define MAXLINES 1000 //global
main()
{
char *lineptr[MAXLINES]; /*pointers to lines*/
//....//
for(i=0; i< nlines; i++)
printf("%s\n", lineptr[i] );
}
int fun_zod(char * sak , char *lineptr[])
{
int len, nlines=0; // String lenght and number of lines
char *p;
char * sep = " "; //Zodziu atskirejas - tarpas
char * zodis = strtok(sak,sep); //Nurodome kad sakini suskirstytume i zodzius po viena
char * zodmas[20];
int i = 0;
zodmas[0] = zodis; //Iraso atskirai kiekviena zodi i masyva
while (zodis != NULL)
{
if (zodis == NULL)
break;
i++;
zodis = strtok(NULL,sep);
zodmas[i] = zodis;
}
int n=i;
for(int j = 0;j < n;j++)
{
if (!( zodmas[j][0] == 'a' || zodmas[j][0] == 'e' || zodmas[j][0] == 'i' || zodmas[j][0] == 'o' || zodmas[j][0] == 'u' ) && !( zodmas[j][1] == 'a' || zodmas[j][1] == 'e' || zodmas[j][1] == 'i' || zodmas[j][1] == 'o' || zodmas[j][1] == 'u' ))
{
zodmas[j]="";
}
}
for (i=0;i < n; i++)
{
len = strlen(&zodmas[i]);
p= malloc(sizeof (char *) * len);
strcpy(p, &line[i]);
lineptr[nlines++] = p;
}
return nlines;
}