2010-01-27 186 views
0

我是新來的c + +編程我不得不用以下參數調用一個函數。 int Start(int argc,char ** argv)。在Visual C++中將1D指針數組(char)轉換爲2D指針數組(char)。

當我嘗試用下面的代碼調用上述函數時,我得到運行時異常。有人能幫我解決上述問題嗎?

char * filename=NULL; 
char **Argument1=NULL; 
int Argument=0; 
int j = 0; 
int k = 0; 
int i=0; 

int Arg() 
{ 
filename = "Globuss -dc bird.jpg\0"; 

for(i=0;filename[i]!=NULL;i++) 
{ 
    if ((const char *)filename[i]!=" ") 
    { 
    Argument1[j][k++] = NULL; // Here I get An unhandled 
          // exception of type 
          //'System.NullReferenceException' 
          // occurred 
     j++; 
     k=0; 
    } 

    else 
    { 
     (const char)Argument1[j][k] = filename [j]; // Here I also i get exception 
     k++; 
     Argument++; 
    } 
} 

Argument ++; 
return 0; 
} 

Start (Argument,Argument1); 

回答

0

你似乎沒有分配任何內存給你數組,你只需要一個NULL指針

char * filename=NULL; 
char **Argument1=NULL; 
int Argument=0; 
int j = 0; 
int k = 0; 
int i=0; 

int Arg() 
{ 
filename = "Globuss -dc bird.jpg\0"; 

//I dont' know why you have 2D here, you are going to need to allocate 
//sizes for both parts of the 2D array 
**Argument1 = new char *[TotalFileNames]; 
for(int x = 0; x < TotalFileNames; x++) 
    Argument1[x] = new char[SIZE_OF_WHAT_YOU_NEED]; 

for(i=0;filename[i]!=NULL;i++) 
{ 
    if ((const char *)filename[i]!=" ") 
{ 
    Argument1[j][k++] = NULL; // Here I get An unhandled 
         // exception of type 
         //'System.NullReferenceException' 
         // occurred 
     j++; 
     k=0; 
    } 

    else 
    { 
     (const char)Argument1[j][k] = filename [j]; // Here I also i get exception 
     k++; 
     Argument++; 
    } 
    } 

    Argument ++; 
    return 0; 
    } 
+0

這是假設你想要一個二維數組,但我不認爲你需要它。如果您希望存儲多個文件名,例如,您需要一個2D數組。 [0] [ 「文件中的一個」] [1] [ 「文件中的兩個」] [2] [ 「文件中的三個」] 但我想你想一維數組 [F] [I] [L] [E] [] [O] [N] [E] – Craig 2010-01-27 07:56:27

+0

嗨克雷格, 感謝您的寶貴意見。我試過了你的建議,但遇到同樣的例外。我的主要動機是稱爲開始(參數,**參數);機能的研究。 – 2010-01-27 08:14:41

+0

你知道兩維數組是如何工作的嗎?我的意思不是聽起來不尊重,而是你想從代碼中看到的是[0] [f] [1] [i] [2] [l] [3] [e],作爲二維數組,你正在分配兩個很大的空間。如果你想要它,所以它是[0] [文件名1] [1] [文件名2],只需從循環中刪除j + +,每次你想添加另一個文件名 – Craig 2010-01-28 00:40:22

0

兩件事情:

char **Argument1=NULL; 

這是指針的指針,你需要在內存中分配一些空間。

*Argument1 = new char[10]; 

for(i=0, i<10; ++i) Argument[i] = new char(); 

不要忘記刪除相同的風格。

0

你必須做的第一件事就是找到你將擁有的字符串的數量。那是易與類似實現:

int len = strlen(filename); 
int numwords = 1; 

for(i = 0; i < len; i++) { 
    if(filename[i] == ' ') { 
     numwords++; 
     // eating up all spaces to not count following ' ' 
     // dont checking if i exceeds len, because it will auto-stop at '\0' 
     while(filename[i] == ' ') i++; 
    } 
} 

在上面的代碼我假定會有在文件名中的至少一個字(即它不會是空字符串)。 現在你可以爲Argument1分配內存。

Argument1 = new char *[numwords]; 

之後,你有兩種選擇:

  1. 使用的strtok(http://www.cplusplus.com/reference/clibrary/cstring/strtok/
  2. 實現你的功能分割字符串

,可以這樣進行:

int i,cur,last; 
for(i = last = cur = 0; cur < len; cur++) { 
    while(filename[last] == ' ') { // last should never be ' ' 
     last++; 
    } 

    if(filename[cur] == ' ') { 
     if(last < cur) { 
      Argument1[i] = new char[cur-last+1]; // +1 for string termination '\0' 
      strncpy(Argument1[i], &filename[last], cur-last); 
      last = cur; 
     } 
    } 
} 

上面的代碼沒有優化,我只是儘量讓它儘可能容易理解。 我也沒有測試它,但它應該工作。假設我提出:

  1. 字符串空終止
  2. 有至少1個字的串英寸

而且每當IM指一個字符串,我的意思是一個字符數組:P

有些錯誤我在代碼中發現:

    在C/C++ 「
  1. 」 是一個指向包含一個空格的const char數組。 如果您將它與另一個「」比較,您將比較指向它們的指針。他們可能(可能會)會有所不同。使用strcmp(http://www.cplusplus.com/reference/clibrary/cstring/strcmp/)。
  2. 您應該學習如何動態分配內存。在c中,你可以用malloc來實現,在C++中用malloc和new來代替malloc。

希望我幫了忙!

PS如果在我的代碼中有錯誤,告訴我,並修復它。