# Passing a String Array from C (C++) into SV using DPI

**URL:** <https://verificationacademy.com/forums/t/passing-a-string-array-from-c-c-into-sv-using-dpi/33572>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, DPI, systemverilog-Arrays\
**Created:** [October 6, 2017, 3:49pm UTC](https://verificationacademy.com/forums/t/passing-a-string-array-from-c-c-into-sv-using-dpi/33572 "2017-10-06T15:49:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbehrin](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mbehrin](https://verificationacademy.com/forums/u/mbehrin)\
**Post date:** [October 6, 2017, 3:49pm UTC](https://verificationacademy.com/forums/t/passing-a-string-array-from-c-c-into-sv-using-dpi/33572/1 "2017-10-06T15:49:48Z")

</div>

I’m trying to pass an array of strings from a DPI function into SystemVerilog in Questa. I have created a simple testcase to experiment with. I can successfully assign string literals to the pointers returned by svGetArrElemPtr1, but if I try to strcpy to them, I get segfaults (see commented lines in the C code). Any suggestions on the correct way to do this?

**C++ Code:**

```verilog
#include <iostream>
#include <svdpi.h>
#include <string>

using namespace std;

extern "C" void strtest(
  svOpenArrayHandle a // String Array Output
) {

    *(char **)svGetArrElemPtr1(a,0) = "Hello";
    *(char **)svGetArrElemPtr1(a,1) = "out";
    *(char **)svGetArrElemPtr1(a,2) = "there";

// const char *mystr = "there";
// strcpy(*(char **)svGetArrElemPtr1(a,count), mystr);
}

```

**SystemVerilog Code:**

```verilog
module strtest_tb;

import "DPI-C" function void strtest(
  output string outbuf[]
);

string str_out[5];

initial begin
  strtest(str_out);
  for (int i=0; i<3; i++) begin
    $display("OUTPUT STRING[%d]:%s", i,str_out[i]);
  end
  $stop;
end

endmodule

```

---

<div class="post-metadata">

**Author:** ![mbehrin](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mbehrin](https://verificationacademy.com/forums/u/mbehrin)\
**Post date:** [October 9, 2017, 4:23pm UTC](https://verificationacademy.com/forums/t/passing-a-string-array-from-c-c-into-sv-using-dpi/33572/2 "2017-10-09T16:23:36Z")

</div>

It looks like I just needed to allocate on the C++ side.

```verilog
*(char **)svGetArrElemPtr1(a,2) = new char[100];
   const char *mystr = "there";
   strcpy(*(char **)svGetArrElemPtr1(a,2), mystr);

```

Problem appears to be solved.

---

<div class="post-metadata">

**Author:** ![prajwalhegde](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/p/9de0a6/32.png) [@prajwalhegde](https://verificationacademy.com/forums/u/prajwalhegde)\
**Post date:** [March 12, 2020, 12:11pm UTC](https://verificationacademy.com/forums/t/passing-a-string-array-from-c-c-into-sv-using-dpi/33572/3 "2020-03-12T12:11:05Z")

</div>

_In reply to [mbehrin](https://verificationacademy.com/forums/t/passing-a-string-array-from-c-c-into-sv-using-dpi/33572/2):_

Thanks for the solution.  
I have a similar problem where i want to write string data to a structure instead of an array. Could you please let me know the way to handle that in both sv and c ?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 13, 2020, 12:15am UTC](https://verificationacademy.com/forums/t/passing-a-string-array-from-c-c-into-sv-using-dpi/33572/4 "2020-03-13T00:15:21Z")

</div>

_In reply to [Prajwal hegde](https://verificationacademy.com/forums/t/passing-a-string-array-from-c-c-into-sv-using-dpi/33572/3):_

```verilog
module top;
   typedef struct {string s1,s2;} st;
   import "DPI-C" function void test (output st a);
   st s; 
   initial begin
      test(s);
      $display("%p",s);
   end
endmodule

```

```auto
#include "dpiheader.h"
extern "C" void test ( st* a ) {
  a->s1 = "Hello";
  a->s2 = "World";
  const char *mystr = "there";
  *(char **)&a->s2 = new char [10];
  strcpy(*(char **)&a->s2, mystr);

}

```
