2010-01-27 184 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如果在我的代码中有错误,告诉我,并修复它。