-3

我希望把下面的代码中的函数:(代码是不完整的,但我认为这应该是清楚的给你)字符* - 阵列作为参数

char *parsedData[SEPERATOR]; 

for(int i=0; i<SEPERATOR; i++) 
{ 
    parsedData[i]=tmp; 
} 

的功能应该像如下:

int main() 
{ 
    char *parsedData[SEPERATOR]; 
    Parser(WTString, parsedData); 
} 
int Parser(char *WTString, *parsedData[SEPERATOR]) 
{ 
    for(int i=0; i<SEPERATOR; i++) 
    { 
     parsedData[i]=tmp; 
    } 
} 

代码在一个函数中工作正常。通过将代码分成两个函数,我没有得到可用的数据。

如果有人能帮助我,我将不胜感激。我不想使用更多的库。

+2

什么是'tmp'? – hmjd

回答

1

char *parsedData[SEPERATOR];为什么?

为什么你需要在C++中使用指向char的原始数组?

为什么你不使用一个std::vector<std::string>并让自己免除一切痛苦和绝望。这样做看起来像这样的

0

A C++方式:

#include <string> 
#include <vector> 

std::vector<std::string> Parser(const char *WTString) 
{ 
    std::vector<std::string> result; 
    for(std::size_t i = 0; i != SEPERATOR; ++i) 
    { 
     result.push_back(tmp); // whatever tmp is 
    } 
    return result; 
} 

我不想使用图书馆的进一步。

别担心,我的代码示例只需要标准库。

0

如果你不想使用STL,我提出这个功能:

int PointToDefault(char* target, char** parsedData, unsigned int count) 
{ 
    for (unsigned int i=0; i<count; i++) 
    { 
     parsedData[i] = target; 
    } 
} 

而且这次调用:

#define SEPERATOR 15 
int main() 
{ 
    char tmp[] = "default string"; 

    char *parsedData[SEPERATOR]; 
    PointToDefault(tmp, parsedData, SEPERATOR); 
} 
+0

它应该没关系。如果计数是一个常数,传递它的意义是什么? – newacct

+0

如果您需要使用它来处理两个不同长度的字符串或在另一个程序中,则不必在函数中更改任何内容(如果是为了工作,编辑后可能需要重新测试整个函数)证明它仍然在做它应该做的)。另外,遵循你的推理,你可以将字符串声明为全局的,在硬编码的函数中引用它们,并省略所有参数,但是,如何将它提取到函数中呢? – SvenS

+0

重点是,这样做对于问题询问 – newacct