I need a sample function to return a dynamic string array which is modified in the function.
THanks...
I need a sample function to return a dynamic string array which is modified in the function.
THanks...
This has been asked dozens of times on this board. Why not try a search?
If you understand what you're doing, you're not learning anything.
Why cant i access the var domains from process_thread() but I can in parser()???
Code:char **add_string( char **list, char *add, int size ) { char **newList; int count; newList = malloc( sizeof( char* ) * ( size + 1 ) ); if( size > 0 ) for( count = 0; count < size; count++ ) { newList[count] = list[count]; } newList[size] = malloc( strlen( add ) + 1 ); strcpy( newList[size], add ); free( list ); return newList; } size_t parser(regex_t *re, struct MemoryStruct *rawhtml, char **domains) { regmatch_t matched[1]; char *domain; size_t i = 0; int m_last = 0; while (regexec(re, &rawhtml->memory[m_last], (size_t) 1, matched, 0) == 0) { strncpy(domain, &rawhtml->memory[matched[0].rm_so + m_last], (matched[0].rm_eo - matched[0].rm_so)); domain[matched[0].rm_eo - matched[0].rm_so - 1] = 0; domains = add_string(domains, &domain[23], i); m_last += matched[0].rm_eo; printf("%s\n", domains[i]); i++; } return(i); } void *process_thread(void *threadargs) { CURLcode res; struct MemoryStruct rawhtml; rawhtml.memory = NULL; rawhtml.size = 0; struct ThreadArg *thread_data; thread_data = (struct ThreadArg *) threadargs; char **domains = NULL; size_t s_domains; if ((res = get_html(thread_data->url, &rawhtml)) == 0) { //Connected and we got the page. if ((s_domains = parser(thread_data->re_domains, &rawhtml, domains))) { int i; for(i=0;i<s_domains;i++) { printf("%s\n", domains[i]); } } } else { //Unable to connect printf("Unable to connect to URL: %s | CURLcode: %i\n", thread_data->url, res); } thread_data->done = 1; --*thread_data->r_threads; pthread_exit(NULL); }
you pass 'domains' by value so the changes you make are performed on a copy of it. change the parameter to a char *** and you will have the variable itself - just dereference the pointer in the function.
O.K. I did what you said and it fixed it, sort of. I added another parser and it stopped working. Below is the code.
Code:char **add_string( char **list, char *add, int size ) { char **newList; int count; newList = (char **) malloc( sizeof( char* ) * ( size + 1 ) ); if( size > 0 ) for( count = 0; count < size; count++ ) { newList[count] = list[count]; } newList[size] = (char *) malloc( strlen( add ) + 1 ); strcpy( newList[size], add ); free( list ); return newList; } size_t domain_parser(const regex_t *re, const struct MemoryStruct *rawhtml, char ***domains) { regmatch_t matched[1]; char *domain; int m_last = 0; int i=0; while (regexec(re, &rawhtml->memory[m_last], (size_t) 1, matched, 0) == 0) { strncpy(domain, &rawhtml->memory[matched[0].rm_so + m_last], (matched[0].rm_eo - matched[0].rm_so)); domain[matched[0].rm_eo - matched[0].rm_so - 1] = 0; (*domains)= add_string((*domains), &domain[23], i); m_last += matched[0].rm_eo; i++; } return(i); } size_t url_parser(const regex_t *re, const struct MemoryStruct *rawhtml, char ***urls) { regmatch_t matched[1]; char *url; int m_last = 0; int ignored_first = 0; int i=0; while (regexec(re, &rawhtml->memory[m_last], (size_t) 1, matched, 0) == 0) { if (ignored_first) { strncpy(url, &rawhtml->memory[matched[0].rm_so + m_last], (matched[0].rm_eo - matched[0].rm_so)); url[matched[0].rm_eo - matched[0].rm_so - 32] = 0; (*urls) = add_string((*urls), &url[8], i); m_last += matched[0].rm_eo; i++; } else { ignored_first = 1; m_last += matched[0].rm_eo; } } return(i); } void *process_thread(void *threadargs) { CURLcode res; struct MemoryStruct rawhtml; rawhtml.memory = NULL; rawhtml.size = 0; struct ThreadArg *thread_data; thread_data = (struct ThreadArg *) threadargs; char **urls = NULL; char **domains = NULL; size_t s_urls=0; size_t s_domains=0; int i; if ((res = get_html(thread_data->url, thread_data->proxy, &rawhtml)) == 0) { //Connected and we got the page. if ((s_domains = domain_parser(thread_data->re_domains, &rawhtml, &domains))) { printf("%i Domains Returned by domain_parser()\n", s_domains); //Executes this with out problems for(i=0;i<s_domains;i++) { printf("%s\n", domains[i]); } } if ((s_urls = url_parser(thread_data->re_urls, &rawhtml, &urls))) { printf("%i URLs Returned by url_parser\n", s_urls); //Segfault here for(i=0;i<s_urls;i++) { printf("%s\n", urls[i]); } } } else { //Unable to connect printf("Unable to connect to URL: %s | CURLcode: %i\n", thread_data->url, res); } free(domains); free(urls); thread_data->done = 1; --*thread_data->r_threads; pthread_exit(NULL); }
The code below works perfectly fine on my machine...
Code:#include <stdio.h> #include <string.h> #include <stdlib.h> char **add_string(char **list, char *add, size_t size) { char **newList; int count; newList = (char **) malloc( sizeof( char* ) * ( size + 1 ) ); if( size > 0 ) for( count = 0; count < size; count++ ) { newList[count] = list[count]; } newList[size] = (char *) malloc( strlen( add ) + 1 ); strcpy( newList[size], add ); free( list ); return newList; } void middle_man(char ***list, char *add, size_t size) { (*list) = add_string((*list), add, size); } int main() { char **p_array = NULL; char **p2_array = NULL; char *t1 = "Line 1"; char *t2 = "Line 2"; char *t3 = "Line 3"; middle_man(&p_array, t1, 0); middle_man(&p_array, t2, 1); middle_man(&p_array, t3, 2); printf("p_array[0]:%s &p_array[0][0]:%p &t1:%p &t1[0]:%p\n", p_array[0], &p_array[0][0], &t1, &t1[0]); printf("p_array[1]:%s &p_array[1][0]:%p &t2:%p &t2[0]:%p\n", p_array[1], &p_array[1][0], &t2, &t2[0]); printf("p_array[2]:%s &p_array[2][0]:%p &t3:%p &t3[0]:%p\n", p_array[2], &p_array[2][0], &t3, &t3[0]); middle_man(&p2_array, t1, 0); middle_man(&p2_array, t2, 1); middle_man(&p2_array, t3, 2); printf("p2_array[0]:%s &p2_array[0][0]:%p &t1:%p &t1[0]:%p\n", p2_array[0], &p2_array[0][0], &t1, &t1[0]); printf("p2_array[1]:%s &p2_array[1][0]:%p &t2:%p &t2[0]:%p\n", p2_array[1], &p2_array[1][0], &t2, &t2[0]); printf("p2_array[2]:%s &p2_array[2][0]:%p &t3:%p &t3[0]:%p\n", p2_array[2], &p2_array[2][0], &t3, &t3[0]); free(p_array); free(p2_array); return (0); }
nevermind I got it... I feel so stupid...
domain_parser()
char domain[100];
url_parser()
char urls[100];
Last edited by fabriciom; 05-17-2005 at 05:55 PM.