Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7947103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:20:11+00:00 2026-06-04T01:20:11+00:00

I am trying to modify a Lua variable in C. I pass Lua userdata

  • 0

I am trying to modify a Lua variable in C. I pass Lua userdata to a C function and the C function uses them and changes them. I want these values changed in Lua as well. Here’s my code –

Lua –

 t = require 'coroutines'
 function foo()
      bar = 0
      foo1 = t.colorHistogram(bar)
      print(bar)
 end

C –

Here I do not know what to put. I read up how the two exchange data. Lua creates a stack that it pushes the arguments into and C accesses those args using the lua_Object. So

 int foo = lua_tonumber(L,1);
 foo = 5;

would initialize foo as 0 and set it to 5. However, the print statement in Lua still displays 0 as the value as it is not the memory where the variable “bar” in Lua is stored that is getting modified. I wanted to know if there is a way to modify the memory. I have also tried replacing the data on the stack with the modified value. That does not work either.

Any help is appreciated. Thank you.

EDIT**

I suppose I was not very clear in my original question. I tried sorry. Anyways, I’ll try better –

What I basically want to do is pass a value to inline C, have C modify that value and be able to read it as modified in Lua. I do no care too much to modify that particular memory location or variable. As long as there is some way for me to read the value that C modified in Lua, I will take that.

 foo1, bar = t.colorHistogram(bar) 

will not work because “t” is a lua function call and colorHistogram is an inline C function in that Lua function. If I pass bar to t.colorHistogram, I would need the Lua function “t” to be able to return bar which would mean I would need the Lua function “t” to read bar as modified. That is what I am not sure how to do. Thanks for your response btw. END EDIT

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:20:12+00:00Added an answer on June 4, 2026 at 1:20 am

    Variables are not passed to functions; values are passed to functions. The number 0 is a value. local bar = 0; t.colorHistogram(bar); is functionally no different from t.colorHistogram(0).

    You cannot manipulate the content of a parameter variable from within a Lua function, because Lua functions are not passed variables. They’re passed the contents of a variable.

    Note that it doesn’t matter whether it’s a C-function or a Lua function; this is just how Lua parameter passing works. Parameters are inputs only; if you want to modify a “variable”, then it either must be a global or an output that the caller stores into that variable:

    bar = 0
    foo1, bar = t.colorHistogram(bar)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to modify a variable from a form. I want to get rid
I am trying to modify pixel values (8 bits per channel RGBA) by numerically
I'm trying to modify a shopping cart (zencart) to do what I want, so
I'm trying to modify the contents of one pointer using another. I want string2
I am currently trying to modify a Javascript function that slides in a <div>.
I'm trying to modify a C# WinForms application that uses multiple forms. At startup,
I'm trying to modify a code not written by me, so I want to
I am trying to modify a function to be more sensible for my purposes;
Hi I'm trying to modify a web page so that it loads faster. Since
I am trying to modify the amcap, an application from Windows SDK's example to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.