如何通过引用传递的数组在C函数? [英] How to pass an array by reference to a function in C?
问题描述
我在C初学者codeR。
我有以下的code:
INT的main(){结构*数组[的malloc(10 *整型尺寸(结构)];
/ *我然后填充结构。 (INT NUM,浮点)
*然后,我想通过这阵,我已经填充到一个sortlist中的功能,
*然后排序数组输出返回主()。
*
*然后我要输出数组为:数点* /
}结构结构
{
INT编号;
浮点;
}无效sortlist中()
{
/ *数组排序,然后返回排序后的数组* /
}
我将如何传递数组,然后回来?
块引用>任何链接或建议是大大有益的。
解决方案
如何按引用传递一个数组...?
块引用>当一个数组
A
传递给一个函数美孚(一)
,它是实际参数的。在C中,当一个数组传递给一个函数,而不是整个阵列被给富()
,前pressionA
被转换到所述阵列的所述第一元素的地址。
除非它是
的sizeof
运算符的操作数,则_Alignof
运营商,或一元&安培;
运营商,或者是用于初始化数组,即具有类型'类型的数组''转换为一个前pression一个前pression一个字符串文字类型'指向数组对象的初始元素,而不是一个左值'指向类型'。 ... C11dr§6.3.2.13
块引用>所以
富()
给定类型的地址为int *
。让我们假设值1234
。在函数
无效美孚(INT * FA)
的形式参数的FA
取值为1234
。从呼叫者的点来看,这是传递按引用为
A
是影响富()
和功能获得了参考。从功能的角度的视图,这是传址值FA
获取转换A
。在C中,正是这第二点看法,即通常所说的人时说,C没有通过引用传递任何东西。在这两种情况下,FA
是为int *
,而不是一个数组。
富()
不得不主要的
A $ C的地址$ C>中的变量
FA
。所以,code发[0] = 456
设置一个后仍被视为一个价值富()
完成。无效美孚(INT * FA){
发[0] = 456;
}诠释主要(无效){
诠释一个[5];
一个[0] = 123;
的printf(%d个\\ N中,[0]); //输出123
富(一);
的printf(%d个\\ N中,[0]); //输出456
返回0;
}我用一个简单的
INT
数组来解释事情。然而,原来的code有其他问题。下面是关于如何分配内存的想法。//结构*数组[的malloc(10 *整型尺寸(结构)];
结构ok12type * ok12Array =的malloc(sizeof的* ok12Array * 10);
....
//做的东西与ok12Array
...
免费(ok12Array);
ok12Array = NULL;
//不要_not_做的东西与ok12ArrayI am a beginner coder in C.
I have the following code:
int main() { struct* Array[malloc(10*sizeOf(struct)]; /* I then fill the struct. (int num,float points) * I then want to pass this array that I've filled up to a sortList function, * which then outputs the sorted array back to main(). * * Then I want to output the array as: "number, points" */ } struct struct { int number; float points; } void sortList() { /*sort Array, then return sorted array*/ }
How would I pass the array and then back?
Any links or suggestions are greatly helpful.
解决方案How to pass an array by reference ...?
When an array
a
is passed to a functionfoo(a)
, it is the actual argument. In C, when an array is passed to a function, rather than the entire array being given tofoo()
, the expressiona
is converted to the address of the first element of the array.Except when it is the operand of the
sizeof
operator, the_Alignof
operator, or the unary&
operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. ... C11dr §6.3.2.1 3So
foo()
is given an address of typeint *
. Let us assume the value is1234
.Inside the function
void foo(int *fa)
, the formal parameterfa
takes on the value1234
.From the caller's point-of view, this is pass-by-reference as
a
is affected byfoo()
and the function received a "reference". From the function's point-of-view, this is pass-by-value asfa
gets a copy of the converteda
. In C, it is this second-point-of view that is usually spoken of when folks says that C does not pass anything by reference. In both cases,fa
is anint *
and not an array.
foo()
had the address ofmain
'sa
in the variablefa
. So codefa[0] = 456
sets a value that is still seen afterfoo()
completes.void foo(int *fa) { fa[0] = 456; } int main(void) { int a[5]; a[0] = 123; printf("%d\n", a[0]); // prints 123 foo(a); printf("%d\n", a[0]); // prints 456 return 0; }
I used a simply
int
array to explain things. Yet original code had other problems. Below are ideas on how to allocate memory.// struct* Array[malloc(10*sizeOf(struct)]; struct ok12type* ok12Array = malloc(sizeof *ok12Array * 10); .... // Do stuff with ok12Array ... free(ok12Array); ok12Array = NULL; // Do _not_ do stuff with ok12Array
这篇关于如何通过引用传递的数组在C函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!