如何将二维数组的字符串传递给C语言的函数?

时间:2013-02-14 20:42:12

标签: c arrays

我无法弄清楚如何将radjectives(二维字符串数组)传递给randomizeadj函数。

#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;
}

2 个答案:

答案 0 :(得分:0)

简单地

randomizeadj(radjectives);

e.g。

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])。< / p>

我改变的其他事情:

answers更改为char指针的数组,而不是char的二维数组,否则编译器将没有为您尝试的值分配内存读入。

char answers[5][100];

还有一个;,不应该在这里:(对于这两个函数)

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

Test program

答案 1 :(得分:0)

你可以按照声明的方式传递它:

将您的函数定义和声明更改为以下内容:

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

注意:对于2D数组,您需要传递cols大小,因此需要传递

修改

并且在您的代码中,您在函数定义中添加了分号(;) char randomizeadj(char adjectives[x][y]); { ... }
char randomizenouns(char nouns[x][y]); { .... }
从功能定义中删除分号


您之前收到的incompatible pointer type错误可能已使用匹配的函数定义签名更改函数声明签名