вопрос : Теперь я получаю исходную строку, я буду получать из нее подстроку, но не разбивать ее, потому что исходная строка используется в будущем.
test.cpp :
void method1(source,char* result)
{
char temp[num];
strcpy(temp,source); //copy the source string
... //use temp
result=target string;
}
void method2(source,char* result)
{
char temp[num];
strcpy(temp,source); //copy the source string
... //use temp
result=target string;
}
int main()
{
char source[];
...(maybe I declare some variables to save the result,e.g. char* result)
method1(source,result);
method2(source,result);
}
это не будет работать, потому что переменная копирования «temp» в методе будет уничтожена перед повторной настройкой.Я получил версию, которая работает: Автор использует структуру для сохранения результата:
typedef struct
{
char* sub[50];
uint32_t num;
}result_t;
Он работает в двух отдельных файлах:
desc_data_parser.h:
#ifndef _DESC_DATA_PARSER_H_
#define _DESC_DATA_PARSER_H_
#include <stdint.h>
class desc_data_parser
{
public:
static const uint32_t MAX_DESC_DATA_NUM=512;
typedef struct
{
char* desc_result[MAX_DESC_DATA_NUM];
uint32_t desc_result_num;
}desc_result_t;
public:
static int lookup_value(char* source,char* key, desc_result_t* results);
static int lookup_value_t(char* source,char* key, char* results);
};
#endif
desc_data_parser.cpp:
#ifndef _DESC_DATA_PARSER_
#define _DESC_DATA_PARSER_
#include "desc_data_parser.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int desc_data_parser::lookup_value(char* source, char* key, desc_result_t* results)
{
printf("yourself\n");
if((NULL == source) || ('\0' ==source[0]) || (NULL == key) || ('\0' == key[0]))
return 0;
int num = 0;
//bzero(results,sizeof(desc_result_t));
char copy_buf[2046]; //the source buf
char* t_buf;
strcpy(copy_buf,source);
t_buf = copy_buf;
//t_buf = strstr(t_buf,key); //keep the buf with key
results->desc_result[0]=copy_buf;
return num;
}
int desc_data_parser::lookup_value_t(char* source, char* key, char* results)
{
printf("yourself\n");
if((NULL == source) || ('\0' ==source[0]) || (NULL == key) || ('\0' == key[0]))
return 0;
int num = 0;
char copy_buf[2046]; //the source buf
char* t_buf;
strcpy(copy_buf,source);
t_buf = copy_buf;
results=copy_buf;
return num;
}
#endif
test2.cpp:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "desc_data_parser.h"
using namespace std;
int main() {
char* source="prod:fdafadfa";
desc_data_parser::desc_result_t temp_result;
memset(&temp_result,0,sizeof(desc_data_parser::desc_result_t));
desc_data_parser::lookup_value(source, "prod:",&temp_result);
printf("%s last result\n",temp_result.desc_result[0]); //why it works
char result[50];
desc_data_parser::lookup_value_t(source, "prod:",result);// why it not
printf("%s last result\n",result);
}
Я пытался объединить test.cpp и test1.cpp вместе, однако это не работает.
У меня есть два вопроса:
- Почему работает вышеуказанный метод?
- Есть ли разница между локально определенными статическими методами и включенными статическими методами?
Я обновил пост, и почему эти два абонента получают разные результаты?