Glad the forum is back... BIG question regarding function pointers and structs...

This is a discussion on Glad the forum is back... BIG question regarding function pointers and structs... within the C++ Programming forums, part of the General Programming Boards category; I am trying to get this program to work, when it runs, any command, aside from exit causes the function ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    Glad the forum is back... BIG question regarding function pointers and structs...

    I am trying to get this program to work, when it runs, any command, aside from exit causes the function mylowercase
    to run. I have no clue, as I have been looking at this for a few days and I cannot find where the problem is. Any help on this one would be huge.
    Code:
     //String Processor with Array of Structures and Commands.cpp : Defines the entry point for the console application.
    #include "stdafx.h"
    #include <cstring>
    
    struct String_Handler{
    	char *name;
    	char *message;
    	char *(*mystringfunction)(char *,const char *);
    };
    char *mystrcopy(char *dst, const char *src){
    	char *rez=dst;
    	for(;(*dst=*src)!='\0';src++,dst++);
    	return rez;
    }
    char *myuppercase(char *dst,const char *src){
    	char *rez=dst;
    	for(;*src!='\0';*dst=(*src>='a'&&*src<='z')?*src+('A'-'a'):*src,src++,dst++);
    	*dst='\0';
    	return rez;
    }
    char *mylowercase(char *dst, const char *src){
    	char *rez=dst;
    	for(;*src!='\0';*dst=(*src>='A'&&*src<='Z')?*src+('a'-'A'):*src,src++,dst++);
    	*dst='\0';
    	return rez;
    }
    char *myrighttrim(char *dst, const char *src){
    	char *rez=dst;
    	for(;*src!='\0'&& *src==' ';src++);
    	for(;(*dst=*src)!='\0';src++,dst++);
    	return rez;
    }
    char *myreverse(char *dst, const char *src){
    	char current_char;
    	char *rez=dst;
    	if(*src!='\0'){
    		current_char=*src;
    		dst=myreverse(dst,src+1);
    		*dst++=current_char;
    		*dst='\0';
    	}
    	return dst;
    }
    int binary_search_iterative(const String_Handler sh_table[],int first_index,int last_index,char *comm){	
    	int middle;
    
    	while(first_index<=last_index){
    		middle=(last_index+first_index)/2;
    		if(strcmp(comm,sh_table[middle].name)==0) return middle;	//successfully found element!
    		if(strcmp(comm,sh_table[middle].name)==-1)
    			last_index=middle-1;
    		else
    			first_index=middle+1;
    	}
    	return -1;		//element not found!
    }
    
    bool string_processor(char *comm,char *dst, char *src, String_Handler sh_table[],int table_size){
    	//for(int i=0;i<table_size&&strcmp(comm,sh_table[i].name)!=0;i++);
    	int i;
    	if(i=binary_search_iterative(sh_table,0,table_size-1,comm)!=-1){
    		std::cout<<sh_table[i].message<<(sh_table[i].mystringfunction)(dst, src)<<std::endl;
    		return true;
    		}
    	else
    		return false;
    }
    void get_command(char *command){
    	std::cout<<"Input command(copy,upper,lower,right,reverse or exit):";
    	std::cin>>command;
    	std::cout<<std::endl;
    	return;
    }
    
    int main()
    {
    	String_Handler string_handler_table[5];
    	
    	string_handler_table[0].name="copy";
    	string_handler_table[0].message="The copy of the string is: ";
    	string_handler_table[0].mystringfunction=mystrcopy;
    
    	string_handler_table[4].name="upper";
    	string_handler_table[4].message="The upper case version of the string is: ";
    	string_handler_table[4].mystringfunction=myuppercase;
    
    	string_handler_table[1].name="lower";
    	string_handler_table[1].message="The lower case version of the string is: ";
    	string_handler_table[1].mystringfunction=mylowercase;
    	
    	string_handler_table[3].name="right";
    	string_handler_table[3].message="The right trimmed version of the string is:";
    	string_handler_table[3].mystringfunction=myrighttrim;
    
    	string_handler_table[2].name="reverse";
    	string_handler_table[2].message="The reversed version of the string is:";
    	string_handler_table[2].mystringfunction=myreverse;
    
    	char *str="             This is the test string!";
    	char rez[100];
    	char command[10]="";
    
    	get_command(command);
    	while(strcmp(command,"exit")!=0){
    		if(!string_processor(command,rez,str,string_handler_table,sizeof string_handler_table/sizeof (String_Handler)))
    			std::cout<<"Not a legal command!"<<std::endl;
    		get_command(command);
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay found your problem...

    This line is bad news!!
    Code:
    if(i=binary_search_iterative(sh_table,0,table_size-1,comm)!=-1)
    I suppose its subtle but this is the root of a lot of problems. You set i = to a function and test it to -1. The function correctly returns the value and it does not equal -1 so it evalutes to true ( or 1 in this case ). You need to wrap your assignment in paranthesis to give correct order of precedence. Here ya go.

    Code:
    if( (i=binary_search_iterative(sh_table,0,table_size-1,comm))!=-1)
    Now you have some other problems but that should get you on the right path. If you get stuck again let us know. Enjoy.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    great, thank you so much, i knew it had to be something so small as assignment and order of operations, but I couldn't find it... sometimes it takes another set of eyes to find what is going on... Appreciate it! Thanks!

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    in the myreversestring function, I'm not sure on the type of loop sturcture to use... obviously the if is not going to cut it, but I thought it would be a place to start, just to see if it prints out the right character... any suggestions?

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    My first suggestion would be to avoid recursion for string processing. It's very wasteful, and an iterative approach is far easier to visualise.
    Code:
    char *myreverse(char *dst, const char *src)
    {
        int srcloc = 0;
        int dstloc = 0;
    
        while (src[srcloc] != '\0')
            srcloc++;
        while (--srcloc >= 0)
            dst[dstloc++] = src[srcloc];
        dst[dstloc] = '\0';
    
        return dst;
    }

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    Thanks!
    Last edited by criticalerror; 02-20-2004 at 08:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulating OOP with structs and function pointers?
    By ITAmember in forum C Programming
    Replies: 31
    Last Post: 05-28-2009, 10:50 PM
  2. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 12:27 AM
  3. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  4. Passing pointers to structs as function arguments
    By Da-Nuka in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2005, 12:10 PM
  5. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 04:29 AM

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