Dynamic String Array

This is a discussion on Dynamic String Array within the C Programming forums, part of the General Programming Boards category; I need a sample function to return a dynamic string array which is modified in the function. THanks......

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    5

    Dynamic String Array

    I need a sample function to return a dynamic string array which is modified in the function.

    THanks...

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    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.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    5
    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);
    }

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    24
    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.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    5
    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);
    }

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    5
    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);
    }

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    5
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array/vector help
    By Cpro in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2008, 03:30 PM
  2. Copying a string, into a string array.
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-31-2006, 05:19 PM
  3. String Compare Using Dynamic Arrays
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 07:01 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21