Sunday, 15 January 2012

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



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

i can't figure out how pass radjectives (2d array of strings) randomizeadj function.

#include<stdio.h> #include<stdlib.h> #include<time.h> #include<ctype.h> char randomizenouns(char[][]); char randomizeadj(char[][]); int main() // origin 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 programme reply several questions used conjure random story length of paragraph.please maintain answers clean.enjoy\n"); printf("\nwhat name?"); scanf("%s\n",answers[0]); printf("\nwhat favorite book?"); scanf("%s",answers[1]); printf("\nwhat favorite color?"); scanf("%s",answers[2]); printf("\nwhat city live in?"); scanf("%s",answers[3]); printf("\nwhat auto drive?"); scanf("%s",answers[4]);

right here lost - cannot figure out how pass radjectives array randomizeadj function.

printf("%s gets lost in %s %s.\n",answers[0],randomizeadj(radjectives[a][b]),answers[1]); printf("%s loves play %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 a(n) %s person likes color %s.\n",answers[0],randomizeadj(radjectives[a][b]),answers[2]); } // end of programme char randomizenouns(char nouns[x][y]); { int x=(rand() %3); int y=(rand() %10); char randomnoun= nouns[x][y]; homecoming randomnoun; } char randomizeadj(char adjectives[x][y]); { int x=(rand() %2); int y=(rand() %7); char randomadjective= adjectives[x][y]; homecoming randomadjective; }

simply

randomizeadj(radjectives);

e.g.

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

at moment things won't compile, alter both declarations , definitions of 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 changed:

changed char[][] (a 2d array of characters) 2d array or character pointers (also note first dimension of array must have length specified).

changed functions homecoming char * rather char (otherwise function returns single character, rather string (but still return adjectives[x][y]).

other things changed:

changed answers not array of char pointers rather 2d array of chars, otherwise compiler won't have memory assigned values you're trying read in.

char answers[5][100];

there's ; there shouldn't here: (for both functions)

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

test program.

c arrays

No comments:

Post a Comment