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

  • Home
  • SEARCH
  • 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 7961421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:01:49+00:00 2026-06-04T05:01:49+00:00

I have spent the past 6 hours trying to solve this ! and i

  • 0

I have spent the past 6 hours trying to solve this ! and i coulnt get anywhere :s

I want to be able to create a lua table in a c++ file and then pass that to a lua script file, which has the following lua function:

function MTable (t) 
local n=#t
    for i=1,n do 
      print(t[i]) 
    end
end

i dynamically created a one dimensional array with two strings:

 lua_newtable(L);
 lua_pushstring(L,"10.10.1.1");
 lua_pushstring(L,"10.10.1.2");
 lua_rawseti(L,-3,2);
 lua_rawseti(L,-2,1);

so now i have the table on top of the stack.
I have verified it by writting this :
if( lua_istable(L,lua_gettop(L)))` which returned 1, which means it is a table.

then I did this:

lua_getglobal(L, "MTable");    // push the lua function onto the stack

uint32_t   result = lua_pcall(L, 1, 0, 0);  //argument 1 is for the table
 if (result) {
 printf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
         exit(1);
}

so I got that error:
Failed to run script: attempt to call a table value

Kindly note that the file has several other functions that i am calling successfully from c++.

can somebody please help me solve this error ? can this be a bug from LUA? cz i followed the steps very correctly…i guess !

  • 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-04T05:01:50+00:00Added an answer on June 4, 2026 at 5:01 am

    The function has to be first on the stack, before the args.

    You can either:

    1. push the function to call on the stack before generating the table, e.g.:

      lua_getglobal(L, "MTable");
      ...generate table on stack...
      int result = lua_pcall(L, 1, 0, 0);
      
    2. Do in the order you do now, and then just swap the arg and the function prior to doing the pcall:

      ...generate table on stack...
      lua_getglobal(L, "MTable");
      lua_insert (L, -2);   // swap table and function into correct order for pcall
      int result = lua_pcall(L, 1, 0, 0);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've spent almost 4 hours trying to get past this issue... I have a
So I have spent the past 11 hours now trying to get even the
Have spent an hour trying to solve this - but to no avail. I'm
I have been trying to fix this issue for the past 2-3 hours but
I've spent the past 3 hours trying to make this work, so sorry if
I have spent hours Googling and searching past questions but have struggled to get
I've spent several hours over the past few days trying to get PostgreSQL to
I have spent the past couple of hours trying all types of variations but
First, I have already spent the past few hours trying to find a solution
I have spent hours in this problem and my fellows couldn't help me out.

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.