2013-02-14 62 views
0

我不知道如何通过radjectives(二维数组的字符串)到randomizeadj函数。如何将二维数组的字符串传递给C语言的函数?

#include<stdio.h> 
#include<stdlib.h> 
#include<time.h> 
#include<ctype.h> 

char randomizenouns(char[][]); 
char randomizeadj(char[][]); 

int main() // beginning of program. 
{ 
    int a=0, b=0; 
    char *answers[5]={'\0'}; 
    char *rnouns[3][10]={'\0'}; 
    char *radjectives[2][17]={'\0'}; 
    char *rcolors[11]={'\0'}; 

    radjectives[0][0]="intriguing"; 
    // ... 
    radjectives[1][6]="loud"; 

    rnouns[0][0]="puppies"; 
    // ... 
    rnouns[1][9]="people"; 

    rcolors[0]="black"; 
    // ... 
    rcolors[10]="orange"; 

    { srand(time(NULL)); 

    printf("\n\tProgram Paragrahs\n"); 
    printf("\tFor this program you will answer several questions which will then be used to  conjure a random story the length of a paragraph.Please Keep your answers clean.Enjoy\n"); 

    printf("\nWhat is your name?"); 
    scanf("%s\n",answers[0]); 
    printf("\nWhat is your favorite book?"); 
    scanf("%s",answers[1]); 
    printf("\nWhat is your favorite color?"); 
    scanf("%s",answers[2]); 
    printf("\nWhat city do you live in?"); 
    scanf("%s",answers[3]); 
    printf("\nWhat car do you drive?"); 
    scanf("%s",answers[4]); 

就在这里,我迷路 - 我无法弄清楚如何将radjectives阵列传递给randomizeadj功能。

printf("%s gets lost in their %s %s.\n",answers[0],randomizeadj(radjectives[a][b]),answers[1]); 
    printf("%s loves to play with %s %s.\n",answers[0],rcolors[(rand() %11)],randomizenouns(rnouns[a][b]);. 
    printf("%s lives in a(n) %s %s.\n",answers[0],randomizeadj(radjectives[a][b]),answers[3]); 
    printf("While living in %s %s drives a(n) %s %s.\n",answers[3],answers[0],rcolors[(rand() %11)],answers[4]); 
    printf("%s is a(n) %s person who likes the color %s.\n",answers[0],randomizeadj(radjectives[a][b]),answers[2]); 
} // end of program 

char randomizenouns(char nouns[x][y]); 
{ 
    int x=(rand() %3); 
    int y=(rand() %10); 

    char randomnoun= nouns[x][y]; 

    return randomnoun; 
} 

char randomizeadj(char adjectives[x][y]); 
{ 
    int x=(rand() %2); 
    int y=(rand() %7); 

    char randomadjective= adjectives[x][y]; 

    return randomadjective; 
} 

回答

0

只需

randomizeadj(radjectives); 

例如

char *adj = randomizeadj(radjectives); 
printf(adj); 

目前事情不会编译,同时改变的声明和功能定义:

char *randomizenouns(char *nouns[3][10]); 
char *randomizeadj(char *adjectives[2][17]); 

或:

char *randomizenouns(char *nouns[][10]); 
char *randomizeadj(char *adjectives[][17]); 

事情我改变:

  • 更改char[][](二维数组字符)到二维数组或字符指针(还要注意数组的第一维必须始终具有指定的长度)。

  • 改变了你的函数返回char *,而不是char(否则你的函数只返回一个字符,而不是字符串(但你仍然只是return adjectives[x][y]

其他的事情我改变。

answers更改为不是char指针的数组,而是使用char s的二维数组,否则编译器将不会为您试图读取的值分配内存。

char answers[5][100]; 

还有一个;那里不应该在这里:(两种功能)

char randomizeadj(char adjectives[x][y]); 
{ 
    ... 

Test program

+0

它仍然说不兼容的指针类型我不知道我做错了什么。 – user2073501 2013-02-14 21:33:31

+0

它表示在我尝试在主体中使用函数radomizeadj(radjectives)的行编译时,不兼容的指针类型。 – user2073501 2013-02-14 21:42:22

+0

@ user2073501请参阅编辑。 – Dukeling 2013-02-14 22:39:40

0

您可以在已宣布的方式简单地将它传递:

改变你的函数定义和声明如下:

char *randomizenouns(char *nouns[rows][cols]) 
char *randomizeadj(char *adjectives[rows][cols]) 

注意:对于二维数组,你需要传递cols大小,所以按要求通过

编辑

在你的代码中你已经把分号(; )函数确定指标
char randomizeadj(char adjectives[x][y]); { ... }
char randomizenouns(char nouns[x][y]); { .... }
remove从函数定义分号


你得到那个incompatible pointer type错误如前面你可能已经改变了函数声明的签名匹配的功能定义签名

+0

它仍然表示不兼容的指针类型 – user2073501 2013-02-14 21:22:06

相关问题