【技术】C语言传递指针到函数 About transfer pointer into function
2013-01-20 23:11
393 查看
About transfer pointer into function (C Language)
In the main() function of our program, we often need to use some user defined functions to do our calculating work.
For a user_function, we may transfer into several int variable, and return a variable.
or, we may need modify a data array by calling a function. And return void.
or, we may need modify a data array in a struct, and call a function to modify that data array. We need to transfer into the function the pointer of that struct containing our data
array.
1. Transfer into function the pointer of An data array:
Take the example of my project: "Audio_Goertzel_Algorithm"
【声数函,方括号;调函数,没括号】
-------------End of section 1. ----------------
Section 2. Transfer the pointer of Struct of a data array
into function:
Take the example of my project: "Piano_demo"
in file "common_data.h", the struct is defined here:
【声结构,方括号;声调函,没括号】
In the main() function of our program, we often need to use some user defined functions to do our calculating work.
For a user_function, we may transfer into several int variable, and return a variable.
or, we may need modify a data array by calling a function. And return void.
or, we may need modify a data array in a struct, and call a function to modify that data array. We need to transfer into the function the pointer of that struct containing our data
array.
1. Transfer into function the pointer of An data array:
Take the example of my project: "Audio_Goertzel_Algorithm"
【声数函,方括号;调函数,没括号】
//--------Some delaration in main() function: --------- float data_Re[1024]; // Array of Real part of data float data_Im[1024]; // Array of image part of data float abs_x[1024]; int length_kArr = 8; // k is array of number of X int kArr[length_kArr]; int num_data = 1024; // Length of array x[i] //--- Prepare the value of k[] ---- kArr[0] = 87; // kArr = [0:1:15] ; length_kArr = 16 kArr[1] = 96; // 770*num_data/fs ... kArr[7] = 204; //--------Process the data by calling the usr_function: goertzel() ---- goertzel(data_Re, data_Im, num_data, kArr, length_kArr); ///////////// The difinition of usr_function in the file "user.c"/////////// void goertzel(float addr_Re[], float addr_Im[], int N, int kArr[], int length_kArr) { // Input the data for processing: // Here we can use "static float array[...]" as global var! float X_Re[1024]; float X_Im[1024]; memcpy( x_Re, addr_Re, 4*N); //float occupy 4 bytes per element. // memcpy(void *dest, void *src, unsigned int count); memcpy( x_Im, addr_Im, 4*N); //float occupy 4 bytes per element. // you can also process addr_Re instead of x_Re, // so that you don't need to memcpy like above. //----Begin of processing ---------- ... //---- End of data processing --- // Output the data processing result: memcpy(addr_Re, X_Re, 4*1024) ; // address_Re store the adr of Re Array of process result memcpy(addr_Im, X_Re, 4*1024) ; // address_Im store the adr of Im Array of process result return; }
-------------End of section 1. ----------------
Section 2. Transfer the pointer of Struct of a data array
into function:
Take the example of my project: "Piano_demo"
in file "common_data.h", the struct is defined here:
【声结构,方括号;声调函,没括号】
struct data_1024res16 { int samplingrate; int datares; int nsamples; int nchannels; int16_t data[1024]; // Here is the data array that we're going to modify. }; //--------Some delaration in main() function: --------- #include"common_data.h" struct data_1024res16 MyStructArr_y[1000]; // Declared 1000 StructArray, each StructArrady contains: //int samplingrate;int datares;int nsamples;int nchannels;int16_t data[1024]; //int16_t data[1024] is 1024 16bit data buffer. //--------Process the data by calling the usr_function: sound_x() ---- // modify the data in MyStructArr_y.data by calling this function sound_x(MyStructArr_y, fs, 494); ///////////// The difinition of usr_function in the file "user.c"/////////// void sound_x(struct data_1024res16* MyStructArr_y, int fs, int F) { short data1[1000]; // use 1000 >> 20 for sufficient. only 20 data1[] are used. //----Begin of processing ---------- ... //---- End of data processing --- // Output the data processing result: for(i = 0; i < 20; i++) { memcpy(MyStructArr_y[i].data, data1, 2*1000); } return; }
相关文章推荐
- C语言中,数组名作为参数传递给函数时,退化为指针
- C语言中,数组名作为参数传递给函数时,退化为指针
- C++ 成员函数指针的问题 pointer-to-member-function
- 081.Return Pointer Function 返回指针的函数
- [iOS]C语言技术视频-15-指针变量高级用法练习一(函数指针完成动态排序)
- c语言 函数 用指针传递参数 问题
- C语言中,数组名作为参数传递给函数时,退化为指针
- (转载) C语言技巧:怎样把数组作为参数传递给函数?(值传递和指针传递)
- c语言--二级指针在函数间的传递和使用
- 函数指针Function Pointer
- c语言的数组、指针、函数传递释义
- C语言 数组做函数参数退化为指针的技术推演
- C语言中函数和指针的參数传递
- Function Pointer of class member function (类的成员函数指针)
- C语言中,数组名作为参数传递给函数时,退化为指针
- C语言-----内存与指针(3) How 4000 do you pass a string to a function如何给一个函数传入字符串作为参数:
- C语言 内存管理--指针的函数传递
- c语言 函数 用指针传递参数 问题
- Function Pointer of class member function (类的成员函数指针)
- 关于C语言通过函数传递多个数据————指针的应用