lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Let explain the difference.  Consider the following code.

local myTableA = { color="blue" }
local myTableB = myTableA
myTableA.color = "red"
print( myTableB.color )

Does this print "blue" or "red"?  It print's "red", because the assignment of myTableA to myTableB did not make a copy of the table.  The assignment causes the two variables to then reference the same table.

local myIntA = 5
local myIntB = myIntA
myIntA = 7
print( myIntB )

What does this print?  It print's "5", because the assignment did make a copy.

Hopefully I didn't screw this up on the mailing list.  My reputation is at stake...haha!

On Wed, Mar 6, 2013 at 11:00 PM, Spencer Parkin <spencer.parkin@gmail.com> wrote:
Some of that looks like C-language, some of that looks like Lua, and I'm not sure what you're asking, but I'll try to help out.  Consider the following Lua code.

local A = { a=1, b=2 }
function Sum( arg )
  return arg.a + arg.b
end
local c = Sum( arg )
print( "c = " .. c )

Assuming I didn't make a mistake, that should print "3".  Notice that variables holding a table value are just pointers to such values.  I don't believe the same is true for variables holding integral or string values, but I don't know much about Lua internals.


On Wed, Mar 6, 2013 at 9:36 PM, Jim zhang <jimzhang02@yahoo.com> wrote:
Code to be test:
struct A
{
   int a,
   int b
};
 
int sum ( struct A * s)
{
   retunr s->a+s->b;
}
 
How could I construct a struct A object in lua scripts, and pass the pointer of this struct into sum() to test it?
 
Thanks,
 
Jim