简体   繁体   中英

How do you pass a 2d array of strings to a function in C language?

I can't figure out how to pass radjectives (2D array of strings) to randomizeadj function.

#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]);

Right here is where I get lost - I cannot figure out how to pass the radjectives array to the randomizeadj function.

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

Simply

randomizeadj(radjectives);

eg

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

At the moment things won't compile, change both the declarations and definitions of the functions to:

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

or:

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

Things I changed:

  • Changed char[][] (a 2D array of characters) to a 2D array or character pointers (also note that the first dimension of an array must always have a length specified).

  • Changed your functions to return char * rather than char (otherwise your function just returns a single character, rather than a string (but you still just return adjectives[x][y] ).

Other things I changed:

Changed answers to not be an array of char pointers but rather a 2D array of char s, otherwise the compiler won't have memory assigned for the values you're trying to read in.

char answers[5][100];

There's also a ; where there shouldn't be here: (for both functions)

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

Test program .

you can simply pass it the way you have declared:

change your function definition and declaration to following:

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

Note : for 2D array you need to pass cols size, so pass as required

EDIT

and in your code you have put semi-colon( ; ) in function defintion
char randomizeadj(char adjectives[x][y]); { ... }
char randomizenouns(char nouns[x][y]); { .... }
remove semi-colon from function definition


you were getting that incompatible pointer type error as earlier on you might not have changed the function declaration signatures with the matching function definition signatures .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM