1. 程式人生 > >loadrunner出現報錯operands of = have illegal types `pointer to char' and `int'

loadrunner出現報錯operands of = have illegal types `pointer to char' and `int'

原始程式碼:

void split(char * p,char * str){
	/*
		傳入一個數組進行p和一個以什麼進行分割的str,返回切片後的值
	*/ 
	
	int i = 0, j = 0;
    char tmp[32][32] = {0};
    char *p1 = (char *)malloc(1024);
 
    while((p1 = strchr(p, *str)) != NULL) //10行
    {
        strncpy(tmp[i], p, strlen(p) - strlen(p1));
        p = p1 + 1;
        i ++
; } strncpy(tmp[i], p, strlen(p)); for(j = 0; j <= i; j++){ lr_output_message("tmp[%d] = %s\n", j, tmp[j]); } } Action (){ char p[] = "www.baidu.com,www.taobao.com,www.csdn.com,www.python.org"; char str[] = ","; //分割的字串 split(p,str); return 0; }

執行後第10行出現指標報錯:operands of = have illegal types `pointer to char’ and `int’ ,百思不得其解,dev-C++中執行一切正常,各種排查後發現傳參確實符合要求,第10行給指標變數賦值時未對strchr返回的值進行強制型別轉換(等於直接給指標變數賦值(太粗心了-_-!!))

修改後指令碼:

void split(char * p,char * str){
	/*
		傳入一個數組進行p和一個以什麼進行分割的str,返回切片後的值
	*/ 
	
	int i = 0, j = 0;
    char tmp[32][32] = {0};
    char *p1 = (char *)malloc(1024);
 
    while((p1 = (char *)strchr(p, *str)) != NULL) //必須使用(char *)進行強制型別轉換
    {
        strncpy(tmp[i], p, strlen(p) - strlen(p1));
        p =
p1 + 1; i ++; } strncpy(tmp[i], p, strlen(p)); for(j = 0; j <= i; j++){ lr_output_message("tmp[%d] = %s\n", j, tmp[j]); } } Action (){ char p[] = "www.baidu.com,www.taobao.com,www.csdn.com,www.python.org"; char str[] = ","; //分割的字串 split(p,str); return 0; } loadrunner中執行結果: Starting iteration 1. Starting action Action. Action.c(19): tmp[0] = www.baidu.com Action.c(19): tmp[1] = www.taobao.com Action.c(19): tmp[2] = www.csdn.com Action.c(19): tmp[3] = www.python.org Ending action Action. Ending iteration 1.