Spiros Bobolakis Spiros Bobolakis - 9 days ago 5
C Question

pass and modify char array[][] in function C

i have a char array[][] and i want to use it at a void function and modify some values.

char teams[MAX_TEAMS][MAX_NAME_LENGTH + 1] = { "Olympiakos", "P.A.O.K.", "Panathinaikos", "Panionios"};


(MAX_TEAMS and MAX_NAME_LENGTH are #defined integers at the start of the programm.
And i was to use a void function to modify the array.How do i have to define the function,how am i goin to pass the array from main?and how am i goin to use it in the function??

void print_teams(char [][]); (?????)

int main(){
char teams[MAX_TEAMS][MAX_NAME_LENGTH + 1] = { "Olympiakos", "P.A.O.K.", "Panathinaikos", "Panionios"};
print_teams(teams);
}

void print_teams(char teams[MAX_TEAMS][MAX_NAME_LENGTH+1]){
strcpy(teams[0], "Milan FC");
}

I am looking for a way of declaration so that any modifies inside the void function to be saved at array teams.
Thanks in advance!

Answer

Best Way to do this :

void print_teams(char [][MAX_COL_SIZE]);

you can find more info in the below link : http://c-faq.com/aryptr/pass2dary.html

and use strcpy(teams[0],"Real Madrid"); instead of using "="