# How to pass an unpacked array from SV to C in DPI export task

**URL:** <https://verificationacademy.com/forums/t/how-to-pass-an-unpacked-array-from-sv-to-c-in-dpi-export-task/30489>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Systemverilog-DPI, unpacked-array\
**Created:** [March 26, 2015, 12:01pm UTC](https://verificationacademy.com/forums/t/how-to-pass-an-unpacked-array-from-sv-to-c-in-dpi-export-task/30489 "2015-03-26T12:01:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kartavya](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kartavya](https://verificationacademy.com/forums/u/kartavya)\
**Post date:** [March 26, 2015, 12:01pm UTC](https://verificationacademy.com/forums/t/how-to-pass-an-unpacked-array-from-sv-to-c-in-dpi-export-task/30489/1 "2015-03-26T12:01:26Z")

</div>

I want to implement a AHB write/read task in SV. It will be called from C.  
So I’m using a DPI and export task.  
Since data can be of any length,it has to be an array.I should be able to pass data for a write command from C to SV.  
We cannot pass open arrays in export tasks,so I want to pass an array of lenght 16 i.e.,data[16] from C to SV.  
How can I pass an unpacked array of type int from C to SV.  
My .sv file has this:

```verilog

export "DPI" task incr_write;
task incr_write(int addr,int unsigned data[16],int size,int len,output resp);
..........
endtask

```

My .c file has this

extern void incr\_write(int,int ???,int,int,int\*);

what should I be using in place of ??? in the above line

Thanks

---

<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 26, 2015, 3:42pm UTC](https://verificationacademy.com/forums/t/how-to-pass-an-unpacked-array-from-sv-to-c-in-dpi-export-task/30489/2 "2015-03-26T15:42:56Z")

</div>

The C header should be

```auto
int incr_write(
    int addr,
    const unsigned int* data,
    int size,
    int len,
    svLogic* resp);

```

The array of int’s will be passed in like in C; you get a pointer to the first element. It would be up to your C code to know there is only 16 elements.

A couple of notes about your task declaration:

- You should be using “DPI-C” as “DPI” has been deprecated. There will eventually be -C++, -SC, -VHDL, etc.
- An exported task has an **int** return value in C that is normally 0. An imported task should also return an int. See 35.9 Disabling DPI tasks and functions
- You last argument is a single bit, not an int. I strongly recommend not relying on implicit types in SystemVerilog.

One last comment about passing open arrays to export tasks. If the potential array size is large, you can skip passing the array in the exported tasks, and instead retrieve the entire array or individual elements through an imported function.

```verilog
import "DPI" context function void get_open_array(inout int data[]);
export "DPI-C" task incr_write;
task incr_write(int addr,int size,int len,output bit resp);
   int data[];
   data = new[size];
   get_open_array(data);
   ...
endtask

```

---

<div class="post-metadata">

**Author:** ![lawrence\_levis](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@lawrence\_levis](https://verificationacademy.com/forums/u/lawrence_levis)\
**Post date:** [August 7, 2017, 11:34pm UTC](https://verificationacademy.com/forums/t/how-to-pass-an-unpacked-array-from-sv-to-c-in-dpi-export-task/30489/3 "2017-08-07T23:34:32Z")

</div>

Can I ask, what about the read case, where the exported dpi must _ **return** _ an array?

How do I write the task? So far, I’ve tried something like the following example, but I C doesn’t see the value. Could be a scoping issue.

```verilog
export "DPI-C" task incr_read;
task incr_read(input int addr, output int data[8], ...);

    data[0] = 0;
    data[1] = 1;
    data[2] = 2;

```
