function-parameter

x86-64 Linux NASM. Function parameter passing of type int array declared as a function in C++ file

我怕爱的太早我们不能终老 提交于 2019-12-12 14:23:40
问题 I tried using this advice for this problem For Linux programming arr[], n, &a, &b are passed in RDI, RSI, RDX and RCX. and the output of the program doesn't sum up the integers in the array properly. It outputs a large number that is obviously wrong. The two files found below were modified from the original 32-bit version found here. http://mcs.uwsuper.edu/sb/224/Intro/c_asm.html What I want is to compile an assembly file that calls a function parameter in a C++ file called array.cpp and then

Dealing with pointer argument in C

谁说胖子不能爱 提交于 2019-12-11 18:24:37
问题 I'm using a library which has a function with the following signature: void LED_stop_blink_task ( void * callback_parameter ); The actual parameter the void pointer stands for is a pointer to uint32_t, which is the number of the led on the board. Is there a way to call this function without using a variable to hold the data ? In my imagination it will be like LED_stop_blink_task(&35); or the only way is like this: uint32_t led_num = 35; LED_stop_blink_task(&led_num); If you're asking why I

How to receive unnamed structures as function parameters in C?

不想你离开。 提交于 2019-12-10 22:16:50
问题 Yesterday while going through this question, I found a curious case of passing and receiving unnamed structures as function parameters. For example, if I have a structure like this, int main () { struct { int a; } var; fun(&var); } Now, what should the prototype of fun be? And how can I use that structure as a structure(pointer) in the function fun ? 回答1: For alignment reasons, this prototype should work: void fun(int *x); And of course: void fun(void *x); I don't see an easy way to actually

Passing inline double array as method argument

我怕爱的太早我们不能终老 提交于 2019-12-10 12:38:06
问题 Consider method functionA (double[] arg) I want to pass a double array inline, like functionA({1.9,2.8}) and not create an array first and then pass it, like double var[] = {1.0,2.0}; functionA(var); Is this possible with C++? Sounds simple, but I could not find a hint anyway concerning my question which made me suspicious :). 回答1: You can do this with std::initializer_list<> #include<vector> void foo(const std::initializer_list<double>& d) { } int main() { foo({1.0, 2.0}); return 0; } Which

c# Enum Function Parameters

家住魔仙堡 提交于 2019-12-09 11:47:48
问题 As a follow on from this question. How can I call a function and pass in an Enum? For example I have the following code: enum e1 { //... } public void test() { myFunc( e1 ); } public void myFunc( Enum e ) { var names = Enum.GetNames(e.GetType()); foreach (var name in names) { // do something! } } Although when I do this I am getting the 'e1' is a 'type' but is used like a 'variable' Error message. Any ideas to help? I am trying to keep the function generic to work on any Enum not just a

C++ : Meaning of const char*const*

帅比萌擦擦* 提交于 2019-12-08 23:25:01
问题 In one of the C++ programs, I saw a function prototype : int Classifier::command(int argc, const char*const* argv) What does const char*const* argv mean? Is it the same as const char* argv[] ? Does const char** argv also mean the same? 回答1: No, it's not the same as const char *argv[] . The const prohibits modifications of the dereferenced value at the particular level of dereferencing: **argv = x; // not allowed because of the first const *argv = y; // not allowed because of the second const

Passing array argument to a function

旧街凉风 提交于 2019-12-08 10:28:44
问题 On calling the function int sum_array(int array[], int arr_length) { int sum = 0; while(--arr_length >= 0) sum += array[arr_length]; return sum; } in main function int main() { int b[10]; ... total = sum_array(b,10); ... } why passing the argument b , not b[] as sum_array(b[],10) ? NOTE : I have no knowledge of pointers. 回答1: In C, arrays are passed as a pointer to the first element. The type of b is array. When passing b , you're actually passing a pointer to the first element of the array.

Classes as parameter of function c++

半城伤御伤魂 提交于 2019-12-08 03:56:11
问题 I wrote a bunch of crypto algorithms as classes and now I want to implement encryption modes (generalized modes shown in wikipedia, not the specific ones in the algorithms' specifications). How would I write a function that can accept any of the classes? edit: here's what i want to accomplish class mode{ private: algorithm_class public: mode(Algorithm_class, key, mode){ algorithm_class = Algorithm_class(key, mode); } }; 回答1: Well, how about template<class AlgorithmType> class mode{ private:

Variadic templates without function parameters

对着背影说爱祢 提交于 2019-12-05 03:11:30
Can I use variadic templates without using the template parameters as function parameters? When I use them, it compiles: #include <iostream> using namespace std; template<class First> void print(First first) { cout << 1 << endl; } template<class First, class ... Rest> void print(First first, Rest ...rest) { cout << 1 << endl; print<Rest...>(rest...); } int main() { print<int,int,int>(1,2,3); } But when I don't use them, it doesn't compile and complains about an ambiguity: #include <iostream> using namespace std; template<class First> void print() { cout << 1 << endl; } template<class First,

scala way to define functions accepting a List of different numeric types

不想你离开。 提交于 2019-12-04 06:30:48
I have the following problem: I have a function which takes a List[Double] as parameter, performs some arithmetic operations on the elements of the list and than return the result. I would like the function also to accept List[Int]. Here is an example: def f(l: List[Double]) = { var s = 0.0 for (i <- l) s += i s } val l1 = List(1.0, 2.0, 3.0) val l2 = List(1, 2, 3) println(f(l1)) println(f(l2)) Of course the second println fails since f requires List[Double] and not List[Int]. Also note the non scala style formulation of the sum within the f function in order to evidence the need to use 0 (or