二维数组,C中的函数调用问题 [英] 2D array, function call question in C
问题描述
嗨
当我调用一个函数并在main函数中创建一个指向2D数组的指针时,我有一个问题。 "主"调用一个函数来输入在main函数中声明的数组中的数据。
Witch工作得非常好,但是我只希望发送我的2D数组的第一行,或者只发送第二行,只发送第三行等。但是。我发送整个数组,并且不知道如何一次只向数组的函数发送一行。在输入功能中,我使用fgets从键盘输入数据。
非常感谢答案
谢谢
Hi
I have a question when I call a function and creating a pointer to a 2D array in the main function. "Main" call a function to input data in a array witch is declared in the main function.
Witch works perfectly well, however I would like only to send the first row of my 2D array, or only the second row, only the third row etc. A.t.m. I send the entire array, and don′t know how to just send one row at a time to the function, of the array. In the input function i input data from the keyboard using fgets.
Would really appreciate an answer
Thanks
推荐答案
如果要发送一行,最好将该行复制到一个新数组中并传递它。
If you want to send one row it is best to copy that row into a new array and pass that.
嗨>
我在调用函数并在main函数中创建指向2D数组的指针时有一个问题。 "主"调用一个函数来输入在main函数中声明的数组中的数据。
Witch工作得非常好,但是我只希望发送我的2D数组的第一行,或者只发送第二行,只发送第三行等。但是。我发送整个数组,并且不知道如何一次只向数组的函数发送一行。在输入功能中,我使用fgets从键盘输入数据。
非常感谢答案
谢谢
Hi
I have a question when I call a function and creating a pointer to a 2D array in the main function. "Main" call a function to input data in a array witch is declared in the main function.
Witch works perfectly well, however I would like only to send the first row of my 2D array, or only the second row, only the third row etc. A.t.m. I send the entire array, and don′t know how to just send one row at a time to the function, of the array. In the input function i input data from the keyboard using fgets.
Would really appreciate an answer
Thanks
因为行在主行的内存中布局,所以只发送单个行非常简单。你不需要复制任何东西。
试试这段代码:
because the rows are laid out in memory in row major, its quite simple to send just the individual rows. you dont need to copy anything.
try this code:
但是我只想发送我的2D数组的第一行,或者只发送第二行,只发送第三行等等。我发送整个数组,并且不知道如何一次只向数组的函数发送一行。
however I would like only to send the first row of my 2D array, or only the second row, only the third row etc. A.t.m. I send the entire array, and don′t know how to just send one row at a time to the function, of the array.
在C和C ++中,您无法将数组传递给函数。函数争论只能是类型或指向类型的指针。数组不是类型。因此,数组是指针。
规则是数组的名称是元素0的地址。当您将数组传递给函数时,只传递地址元素0.
考虑这个:
In C and C++ you cannot pass an array to a function. Function arguiments can only be a type or a pointer to a type. An array is not a type. Therefore, an array is a pointer.
The rule is that the name of the array is the address of element 0. When you pass the array to a funciton, you pass only the address of element 0.
Consider this:
这篇关于二维数组,C中的函数调用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!