2016-11-28 146 views
0

我有一個用c編寫的代碼。我有一個int指針,並將其傳遞給一個函數。該函數分配內存並填充數組,然後返回。基本上看起來是這樣的:如何在C中動態創建一個函數列表#

main() 
{ 
    int* array; 
    function(&array); 
} 

void function(int** array) 
{ 
    int size = 25; 
    *array = malloc(size); 
    (*array)[0] = 42; 
} 

尺寸在主體中是未知的。我如何在C#中執行此操作?我嘗試了List,但我無法使它工作。我已經嘗試了列表和ref列表,並且它們都給索引超出範圍。

編輯:

這工作得很好

class Program 
{ 
    static void function(List<int> array) 
    { 
     array.Add(42); 
    } 

    static void Main(string[] args) 
    { 
     List<int> array = new List<int>(); 
     function(array); 
    } 
} 

而這其中也

class Program 
{ 
    static void function(out int[] array) 
    { 
     array = new int[25]; 
     array[0] = 42; 
    } 

    static void Main(string[] args) 
    { 
     int[] array; 
     function(out array); 
    } 
} 

但下面拋出一個異常

class Program 
{ 
    static void function(out List<int> array) 
    { 
     array = new List<int>(25); 
     array[0] = 42; 
    } 

    static void Main(string[] args) 
    { 
     List<int> array; 
     function(out array); 
    } 
} 
+2

_「我嘗試過使用List,但是我無法使它工作。」_您的嘗試在哪裏? – JLRishe

+0

「list」引發的錯誤是什麼,因爲我沒有看到/預防列表使用相同/相似方式的任何問題。 – Jaya

+0

請告訴我們你已經嘗試了什麼。 – ja72

回答

0

具有用於此目的的ref關鍵字。儘管在這種情況下它不是必需的,因爲無論如何,數組仍然是一個引用類型,但它是一個好主意,因爲它傳達了設計意圖。更好的做法是使用列出的關鍵字作爲函數的參數,不需要在調用之前初始化。

嘗試這種情況:

class Program 
{ 
    static void Main(string[] args) 
    { 
     int[] a; 
     function(out a); 
     Debug.WriteLine(a.Length); 
    } 

    public static void function(out int[] array) 
    { 
     array=new int[25]; 
     array[0]=42; 
    } 
} 

編輯1 - 與返回的數組替代方式

class Program 
{ 
    static void Main(string[] args) 
    { 
     int[] a = CreateArray(); 
     Debug.WriteLine(a.Length); 
    } 

    public static int[] CreateArray() 
    { 
     int[] array=new int[25]; 
     array[0]=42; 
     return array; 
    } 
} 

編輯2 - 具有兩個參數實施例

class Program 
{ 
    static void Main(string[] args) 
    { 
     int[] akw, zuk; 
     function(out akw, out zuk); 
     Debug.WriteLine(akw.Length); 
     Debug.WriteLine(zuk.Length); 
    } 

    public static void function(out int[] A, out int[] B) 
    { 
     A=new int[25]; 
     B=new int[15]; 
     A[0]=42; 
     B[0]=21; 
    } 
} 
+0

將數組作爲返回值而不是out參數返回可能會更好。 – Wazner

+0

我堅持使用_OP_代碼,但是,我會用函數返回來完成它。我編輯了代碼來展示這一點。 – ja72

+0

我已閱讀評論,現在就試用。但首先,返回數組不是一個選項,因爲我可能想要傳遞至少兩個數組到該函數。我不想將僞代碼與額外的細節混爲一談。 – Junipella

1
Class Demo 
{ 


static void main (string[] args) 
{ 
    var result=Add(); 
    Console.WriteLine(result[0]); 
} 

    static List<int> Add() 
    { 
    var listOfints= new List<int>(); 
    listOfints.Add(42); //either this or declare an array of integers and get it initialized by reading the user value and pass the same as param here to initialize and return the array 
    return listOfints; 
    } 

} 
+0

我不喜歡'_listOfints'是一個在Add()方法中初始化的字段。我認爲這是不好的編碼。它也沒有和_OP_一樣的意圖,這個數組本身就是'main()'。 – ja72

+0

當然 - 我儘可能快地提出了一些幫助,因此替代方案我在我的評論中建議聲明一個局部變量並返回相同的結果。 'Out'可能是最接近的事情,但是想到返回本地的東西會是更好的選擇 – Jaya

+0

@ ja72好的,根據你的評論,我不否認我改變了我的代碼。我再一次想到了這件事,以便開始。 – Jaya

0

我認爲你有問題將值[0]分配給c#列表的原因是,除非你創建一個具有預定義開始大小的列表<>集合,它將是空的。因此,entry [0]爲空,不能設置。

如果你想直接訪問元素0,使用此:

void function(out List<int> list) 
    { 
    list = new List<int>(25); 
    list[0] = 42; 
    } 

相反,如果你不需要/知道名單的初始分配的大小,使用:(推薦恕我直言)

void function(out List<int> list) 
    { 
    list = new List<int>(); 
    list.Add(42); 
    } 

祝你好運!

0

另一種方式(儘管它建議使用List<int>

更換int *out int[]

public class Program 
{ 
    public static void Main() 
    { 
     int[] array; 
     function(out array); 
     Console.WriteLine(array[0]); 
    } 

    static void function(out int[] array) 
    { 
     int size = 25; 
     array = new int[size]; 
     array[0] = 42; 
    } 
} 

輸出

42 
0

如上C#中提到使用引用。指針是支持的,但你需要編寫託管代碼..這裏 https://msdn.microsoft.com/en-us/library/14akc2c7.aspx


更多信息有關引用示例如下。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace alistnamespace 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<int> my_list=new List<int>(); 
      functionaddtolist(ref my_list); 
      for(int i=0;i<my_list.Count;i++) 
      { 
       Console.WriteLine("List item " + i.ToString() + "=" + my_list[i]); 
      } 
     } 

     static void functionaddtolist(ref List<int> mylist_ref) 
     { 
      mylist_ref.Add(42);    //Add one element 

     } 
    } 
} 
相關問題