how to access unknown parameters passed using ... to a function in c - c

Sometimes when we don't know how many and which parameters need to be passed to a function, then we pass it using three periods (ellipsis) ... but how to access these parameters inside the function?

Related

Does "inout" affect array's copy on write behaviour?

I think inout makes you passes in a reference (is that accurate?), then if the reference gets changed many times, as you might do with an array, the array then does not have to copied many times because its now a reference type?
The semantics for in-out parameters in swift is different from passing value by reference. Here's exactly what happens when you're passing an in-out parameter:
In-out parameters are passed as follows:
When the function is called, the value of the argument is copied.
In the body of the function, the copy is modified.
When the function returns, the copy’s value is assigned to the original argument.
This behavior is known as copy-in copy-out or call by value result. For example, when a computed property or a property with observers is passed as an in-out parameter, its getter is called as part of the function call and its setter is called as part of the function return.
See https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-ID545
Array is value type in swift so it's fully copied in this scenario. Of course the swift compiler may optimize that but anyway you're guaranteed to see exact same behavior as it'd be with full copies performed.
If you want to pass an array by reference and allow the called function to modify elements quickly, you have the choice of either explictly creating an NSMutableArray, or creating a class where instances have an array as their single member.

How to make apply() pass the object in one of the arguments of the function (not the first)?

I have a some high dimensional arrays and want to apply some functions to them. The problem is I cannot tell apply() (or similar in apply family) that the object to pass the function on is not in the first argument of the function.
Dumb example here:
data <- c(1,2,3,4,5)
myarray <- array(data,dim=c(5,5,2,2),dimnames=list(NULL,
paste("col",1:5,sep=""),
paste("matrix",1:2,sep=""),
paste("slice",1:2,sep="")))
Now, imagine a function with this structure:
function(arg1,arg2,arg3,data,arg4)
If I want to make apply pass the function to the object "myarray", I need to specify on which argument ("data") is located. I tried this but with no results:
apply(myarray,c(3,4),function,arg1,arg2,arg3,arg4)
Thanks in advance!
If I have understood your right, you need to specify the non iterating arguments. For example:
func <- function(a,b,c){
return(a*b + c)
}
apply(FUN=func,a=10,b=10,someObject)
The non specified argument is the argument that is iterated over with your specified vector.
Note that if you have a 1D structure
unlist(lapply(FUN=func,a=10,b=10,someObject))
Will likely work better.

passing GtkWidget parameters

I have a function that is connected to the "changed" signal for a combobox in gtk, which in turn calls another function read_button_config.
The read_button_config takes the main window, a combobox and a vcontainer which I had not declared globally as parameters
Which way is better for passing these parameters:
- declaring the parameters globally
- passing the parameters as a struct to the first function, and passing the struct members to the read_button_config function?
Please let me know why you think either is better, I want to know what is a better way for future development
Which way is better for passing these parameters: - declaring the
parameters globally - passing the parameters as a struct to the first
function, and passing the struct members to the read_button_config
function?
The latter is the preferred way; that is the purpose of the user_data parameter to signal functions, after all.
Declaring them globally isn't bad. Rather, using the user_data is idiomatic. It's a bit harder since you have to manage the memory for the user_data yourself, but using user_data keeps your code modules more self-contained. You will need to decide what you want to do.
– andlabs

Using C variable inside Lua alongside nested functions

This is a sort of followup to my previous question about nested registered C functions found here:
Trying to call a function in Lua with nested tables
The previous question gave me the answer to adding a nested function like this:
dog.beagle.fetch()
I also would like to have variables at that level like:
dog.beagle.name
dog.beagle.microchipID
I want this string and number to be allocated in C and accessible by Lua. So, in C code, the variables might be defined as:
int microchipIDNumber;
char dogname[500];
The C variables need to be updated by assignments in Lua and its value needs to be retrieved by Lua when it is on the right of the equal sign. I have tried the __index and __newindex metamethod concept but everything I try seems to break down when I have 2 dots in the Lua path to the variable. I know I am probably making it more complicated with the 2 dots, but it makes the organization much easier to read in the Lua code. I also need to get an event for the assignment because I need to spin up some hardware when the microchipIDNumber value changes. I assume I can do this through the __newindex while I am setting the value.
Any ideas on how you would code the metatables and methods to accomplish the nesting? Could it be because my previous function declarations are confusing Lua?
The colon operator (:) in Lua is used only for functions. Consider the following example:
meta = {}
meta["__index"] = function(n,m) print(n) print(m) return m end
object = {}
setmetatable(object,meta)
print(object.foo)
The index function will simply print the two arguments it is passed and return the second one (which we will also print, because just doing object.foo is a syntax error). The output is going to be table: 0x153e6d0 foo foo with new lines. So __index gets the object in which we're looking up the variable and it's name. Now, if we replace object.foo with object:foo we get this:
input:5: function arguments expected near ')'
This is the because : in object:foo is syntactic sugar for object.foo(object), so Lua expects that you will provide arguments for a function call. If we did provide arguments (object:foo("bar")) we get this:
table: 0x222b3b0
foo
input:5: attempt to call method 'foo' (a string value)
So our __index function still gets called, but it is not passed the argument - Lua simply attemps to call the return value. So don't use : for members.
With that out of the way, let's look at how you can sync variables between Lua and C. This is actually quite involved and there are different ways to do it. One solution would be to use a combination of __index and __newindex. If you have a beagle structure in C, I'd recommend making these C functions and pushing them into the metatable of a Lua table as C-closures with a pointer to your C struct as an upvalue. Look at this for some info on lua_pushcclosure and this on closures in Lua in general.
If you don't have a single structure you can reference, it gets a lot more complicated, since you'll have to somehow store pairs variableName-variableLocation on the C side and know what type each is. You could maintain such a list in the actual Lua table, so dog.beagle would be a map of variable name to one or two something's. There a couple of options for this 'something'. First - one light user data (ie - a C pointer), but then you'll have the issue of figuring out what that is pointing to, so that you know what Lua type to push in for __index and what to pop out for __newindex . The other option is to push two functions/closures. You can make a C function for each type you'll have to handle (number, string, table, etc) and push the appropriate one for each variable, or make a uber-closure that takes a parameter what type it's being given and then just vary the up-values you push it with. In this case the __index and __newindex functions will simply lookup the appropriate function for a given variable name and call it, so it would be probably easiest to implement it in Lua.
In the case of two functions your dog.beagle might look something like this (not actual Lua syntax):
dog.beagle = {
__metatable = {
__index = function(table,key)
local getFunc = rawget(table,key).get
return getFunc(table,key)
end
__newindex = function(table,key,value)
local setFunc = rawget(table,key).set
setFunc(table,key,value)
end
}
"color" = {
"set" = *C function for setting color or closure with an upvalue to tell it's given a color*,
"get" = *C function for getting color or closure with an upvalue to tell it to return a color*
}
}
Notes about the above: 1.Don't set an object's __metatable field directly - it's used to hide the real metatable. Use setmetatable(object,metatable). 2. Notice the usage of rawget. We need it because otherwise trying to get a field of the object from within __index would be an infinite recursion. 3. You'll have to do a bit more error checking in the event rawget(table,key) returns nil, or if what it returns does not have get/set members.

different methods in passing parameters in Cakephp

I am using cakephp v1.26.
I got a function in a controller like this:
class testingsController extends AppController{
function testing($id=null){
$recieved = $id;}
}
I am not sure if there are any better ways to pass a parameter to the Action testing.
But I have come across some web sites and got these two methods.
Is there any difference in the following parameter passing methods?
1. url/testings/testing/1
2. url/testings/testing:1
url/testings/testing/1
With standard routes, this will call TestingsController::testing(1).
This is standard parameter passing, any parameters beyond /:controller/:action/ are passed "as-is" to the called action.
/controllers/action/param1/param2 corresponds to
ControllersController::action($param1, $param2)
url/testings/testing:1
With standard routes, this will call TestingsController::index() and
set $this->params['named']['testing'] to 1. This is known as a named parameter.
Named parameters can be passed in any order. These two URLs are equivalent:
url/testings/testing:1/foo:2
url/testings/foo:2/testing:1
They will not be passed to the function, as in function testing($id = null). $id will be null. They're only available in the $this->params['named'] array.
The first example you have will pass it as a numeric parameter
$this->params[0]; // 1
The second will pass a named pair, rather like an array
$this->params['testing']; // 1
You can use either for different things. You'll notice that the paginator uses key:val paired parameters when sorting columns and pages.
There is a little bit of further info in the Book, http://book.cakephp.org/2.0/en/development/routing.html#passed-arguments

Resources