Thread: passing string array to a function

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    10

    Question passing string array to a function

    Hi
    I am trying to pass the string array to a function print_it(). I get the following error:

    Error: Reference out of bound array word
    Trying to take a reference to an array word with an out of bounds index

    I think I am making some basic mistake in passing string array to the function. Can anyone please help me out???

    Sorry, the code is written in system verilog but it is similar to C++....

    The code is shown below:

    Code:
    class A;
    
    string mode1[3] = '{"chip", "boundary", "chain"};
    string mode2[2] = '{"chip", "chain"};
    
    endclass : A
    class B;
    
    function void print_it(string details[]);
    A test_mode;
    test_mode = new();
    foreach(details[i])
     begin
    test_mode.mode1=xxx
    $display(details[i]);
    end
    endfunction: print_it
    
    endclass : B
    
    class C;
    
    A test_mode;
    B printing;
    
    printing.print_it(test_mode.mode1);
    
    endclass:C

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, what is your C++ code? If you really mean to say that you are programming in Verilog instead, then this is not the right place to be asking this question.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    10
    I am programming in system verilog.. but the problem I am getting is similar to C++... passing the string array to function is similar as we do in C++... So my point is that the code is similar to C++

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *Thread moved to Tech board*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by debugg
    but the problem I am getting is similar to C++... passing the string array to function is similar as we do in C++... So my point is that the code is similar to C++
    Yes, some things are similiar, but the language is sufficiently different that, as a C++ programmer with no prior knowledge of system verilog, I cannot help you beyond telling you that you are probably accessing an array out of bounds, but you probably already guessed this from the error message.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    10
    ok. so let me put it in this way now.

    Code:
    int main(); { string mode1[3] = '{"chip", "boundary", "chain"}; string mode2[2] = '{"chip", "chain"}; print_it(mode1); } function void print_it(string details[]); foreach(details[i]) begin test_mode.mode1=xxx $display(details[i]); end endfunction: print_it
    If I do something like above, I get the same error. The problem is only with passing string array as an argument to the function. I dont know what I am missing in it now???

  7. #7
    Registered User
    Join Date
    Jun 2012
    Location
    Norway
    Posts
    9
    in C you could do:

    Code:
    void print_it(string* details, int sizeOfArray)
    {
       for( int i=0; i<sizeOfArray; i++ )
       {       cout << detailts[i] << endl;
       }
    }
    no idea if it works in verhilog tho

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    10
    @opply
    thanks a lot for the reply..

    Is there any other way we can write the following line in C++:
    Code:
    void print_it(string* details, int sizeOfArray)
    


    I tried to do something like following but it gives me syntax error:
    Code:
    void print_it(string details[int sizeOfArray])
    


  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by debugg
    Is there any other way we can write the following line in C++:
    I might write it as:
    Code:
    void print_it(const vector<string>& details)
    because the vector<string> comes with size information, but has nothing to do with system verilog. Then there are other, more generic ways, but again this may not be applicable to the problem you are trying to solve in system verilog.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-24-2011, 12:35 PM
  2. Passing a string to and from a function.
    By Obe in forum C Programming
    Replies: 10
    Last Post: 05-19-2010, 04:26 PM
  3. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  4. passing string to function
    By R.Stiltskin in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 12:56 PM
  5. Passing string through a function...
    By MisterWonderful in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2003, 01:33 PM