argument-passing

Why is list when passed without ref to a function acting like passed with ref?

对着背影说爱祢 提交于 2019-11-27 05:24:51
If I did not get this terribly wrong, this behaviour is strange for me. Rather than explaining, I'll post a sample code below and please tell me why does I get output x and not y. private void button1_Click(object sender, EventArgs e) { List<int> l = new List<int>() { 1, 2, 3 }; Fuss(l); MessageBox.Show(l.Count.ToString()); } private void Fuss(List<int> l) { l.Add(4); l.Add(5); } Output should, I assume would be 3. But I get the output as 5. I understand the output can be 5 if I do this: private void button1_Click(object sender, EventArgs e) { List<int> l = new List<int>() { 1, 2, 3 }; Fuss

Passing parameters dynamically to variadic functions

跟風遠走 提交于 2019-11-27 05:21:30
I was wondering if there was any way to pass parameters dynamically to variadic functions. i.e. If I have a function int some_function (int a, int b, ...){/*blah*/} and I am accepting a bunch of values from the user, I want some way of passing those values into the function: some_function (a,b, val1,val2,...,valn) I don't want to write different versions of all these functions, but I suspect there is no other option? Variadic functions use a calling convention where the caller is responsible for popping the function parameters from the stack, so yes, it is possible to do this dynamically. It's

Is there a difference between main(String args[]) and main(String[] args)?

巧了我就是萌 提交于 2019-11-27 04:17:19
问题 Is there a difference between: public void main(String args[]) { ... } and public void main(String[] args) { ... } I don't believe so, but I am wondering. 回答1: Semantically, they are identical. However, I'd recommend using the latter syntax ( String[] args ) when declaring arrays. The former syntax is there mainly for compatibility with C syntax. Since String[] , as a whole, is the type of the object in Java, it's more consistent and clear not to split it up. A similar question addresses the

Using command-line argument for passing files to a program

非 Y 不嫁゛ 提交于 2019-11-27 02:54:00
问题 How can I receive a file as a command-line argument? 回答1: Just the path of the file is passed, inside your program use the Java File class to handle it This takes the first parameter as the file path: import java.io.File; public class SomeProgram { public static void main(String[] args) { if(args.length > 0) { File file = new File(args[0]); // Work with your 'file' object here } } } 回答2: in Java, the main method receives an array of String as argument, as you probably have noticed. you can

passing parameters to bash when executing a script fetched by curl

妖精的绣舞 提交于 2019-11-26 23:53:36
问题 I know how to execute remote bash script, via these syntaxes: curl http://foo.com/script.sh | bash or bash < <( curl http://foo.com/script.sh ) which give the same result. But what if I need to pass arguments to the bash script ? It's possible when the script is saved locally: ./script.sh argument1 argument2 I tried several possibilities like this one, without success: bash < <( curl http://foo.com/script.sh ) argument1 argument2 回答1: try curl http://foo.com/script.sh | bash -s arg1 arg2 bash

void pointer as argument [duplicate]

Deadly 提交于 2019-11-26 22:05:27
问题 This question already has an answer here: Dynamic memory access only works inside function 1 answer The following C snippet: [...] void f1(void* a){ printf("f(a) address = %p \n",a); a = (void*)(int*)malloc(sizeof(int)); printf("a address = %p \n",a); *(int*)a = 3; printf("data = %d\n",*(int*)a); } void f(void){ void* a1=NULL; printf("a1 address = %p \n",a1); f1(a1); printf("a1 address = %p \n",a1); printf("Data.a1 = %d\n",*(int*)a1); } [...] results in a1 address = (nil) f(a) address = (nil)

Functions with a flexible list of ordered/unordered and labeled/unlabeled inputs in MATLAB

给你一囗甜甜゛ 提交于 2019-11-26 21:26:43
问题 A lot of MATLAB functions have an input structure such as: output = function MyFun(a,b,c,'-setting1',s1,'-setting2',s2,'-setting3',s3) I am wondering how I should implement this kind of functionality in my own functions. To be precise, I would like to find out how I can create a function such that: The function has a variable number of inputs N + M The first N inputs are ordered and unlabeled . In the example above, N = 3 . The first input is always a , second input is always b , third input

Function pointer as an argument

自古美人都是妖i 提交于 2019-11-26 19:08:24
问题 Is it possible to pass a function pointer as an argument to a function in C? If so, how would I declare and define a function which takes a function pointer as an argument? 回答1: Definitely. void f(void (*a)()) { a(); } void test() { printf("hello world\n"); } int main() { f(&test); return 0; } 回答2: Let say you have function int func(int a, float b); So pointer to it will be int (*func_pointer)(int, float); So than you could use it like this func_pointer = func; (*func_pointer)(1, 1.0); /

Argument passing strategy - environment variables vs. command line

不羁的心 提交于 2019-11-26 17:58:44
问题 Most of the applications we developers write need to be externally parametrized at startup. We pass file paths, pipe names, TCP/IP addresses etc. So far I've been using command line to pass these to the appplication being launched. I had to parse the command line in main and direct the arguments to where they're needed, which is of course a good design , but is hard to maintain for a large number of arguments. Recently I've decided to use the environment variables mechanism. They are global

Check inside method whether some optional argument was passed

こ雲淡風輕ζ 提交于 2019-11-26 17:13:15
问题 How do I check if an optional argument was passed to a method? public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) { if (optionalint was passed) return; } Another approach is to use Nullable<T>.HasValue (MSDN definitions, MSDN examples): int default_optionalint = 0; public void ExampleMethod(int required, int? optionalint, string optionalstr = "default string") { int _optionalint = optionalint ?? default_optionalint; } 回答1: Well, arguments are