2013-11-14 73 views
0

林與尋找串開始在u_char *, IM試圖讓「Q =」解析從URL 這谷歌搜索的位置掙扎是推動我瘋了,我真的很感激,如果有人能幫助我。 我的C知識是有限的。C:從u_char解析字符串*

這行IM停留在:我的代碼if(g_start = strstr(final_url, "q=") != NULL){

這裏是部分:

int url_length,host_length, search_length; 
u_char *url, *end_url, *final_url, *host, *end_host, *final_host, *ghostUrl, *google, *g_end, *g_start; 


/* copy host + url to a null terminated c string */ 
final_url = (u_char*)malloc(url_length + 1); //malloc (length) +1 for \0 
final_host = (u_char*)malloc(host_length +1); 
strncpy((char*)final_host,(char*)host, host_length); 
strncpy((char*)final_url, (char*)url, url_length); 
final_url[url_length] = '\0'; //null terminerer 
final_host[host_length] = '\0'; 

    /* Everything above works fine.. */ 
    /* final_url looks like this: /search?biw=320&bih=570&tbm=isch&sa=1&ei=M0eFUsXvHOO54AS644GYCQ&q=dogs&oq=dogs&gs_l=mobile-gws-serp.12...0.0.0.1991659.0.0.0.0.0.0.0.0 */ 

if(strcmp("www.google.no", final_host) == 0){ /* host: www.google.no */ 
    if(g_start = strstr(final_url, "q=") != NULL){ /* This fails.. */  
printf("debug: search starts at %s\n", g_start); /* I have no idea.. how to get integer position from strstr() */ 
    g_end = strchr((char*)g_start,'&'); // ?q=dogs[&] 
    search_length = g_end - g_start; 

    printf("DEBUG: search_length: %i\n",search_length); 
    google = (u_char*)malloc(search_length +1);  
    strncpy((char*)google, (char*)g_start, search_length); 
    google[search_length] = '\0'; //null terminator 

回答

1

Operator precedence意味着

if(g_start = strstr(final_url, "q=") != NULL) 

作爲

if(g_start = (strstr(final_url, "q=") != NULL)) 
評價

將分配10g_start

你需要使用括號()圍繞該行的開始得到的評價您所需要的順序:

if((g_start = strstr(final_url, "q=")) != NULL) 
//^        ^

注意你的編譯器應該警告你這一點。例如MSVC給

warning C4047: '=' : 'char *' differs in levels of indirection from 'int' 
+0

我的男人!非常感謝兄弟,我真的很感激它!我終於可以去睡覺了:) – eirik