简体   繁体   English

在函数C中传递并修改char array [] []

[英]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数组[] [],我想在void函数上使用它并修改一些值。

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?? (MAX_TEAMS和MAX_NAME_LENGTH是程序开头的#defined整数。而且我要使用void函数来修改数组。如何定义函数,如何从main传递数组?以及如何我要在功能中使用它吗?

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!

Inside the function teams[0] is an array. 在功能teams[0]是一个数组。 You can't assign to an array, only copy to it: 您不能分配给数组,只能复制到数组:

strcpy(teams[0], "Milan FC");

Also note the use of double quotes. 还请注意使用引号。

Lastly for the function prototype, just copy the declaration you have for the function definition. 最后,对于函数原型,只需复制函数定义中的声明即可。

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 您可以在以下链接中找到更多信息: http : //c-faq.com/aryptr/pass2dary.html

and use strcpy(teams[0],"Real Madrid"); 并使用strcpy(teams[0],"Real Madrid"); instead of using "=" 而不是使用“ =”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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