Need advice

This is a discussion on Need advice within the C++ Programming forums, part of the General Programming Boards category; hi, I need some help on the following issue. how should I declare a C/C++ function to return "char *temp[] ...

  1. #1
    trapped
    Guest

    Need advice

    hi,
    I need some help on the following issue.

    how should I declare a C/C++ function to return
    "char *temp[] "

    I tried to use char **, unfortunately it does not work.

    basically, I want to achieve this. suppose temp = {"abc", "de","2"}

    I want a function to be able to return a pointer so that I could
    achieve "abc", "de", "2" one by one.

    thanks

  2. #2
    trapped
    Guest

    here is the code

    as you can see when you debug, Temp has every thing.
    but test_output is unable to do the job, I can only call this
    function once.

    thanks for the help

    #include <iostream.h>
    #include <string.h>

    const int LIST_LENGTH = 10;
    const int STRING_LENGTH = 100;
    char CHAR_ARRAY[STRING_LENGTH];
    char *Temp[LIST_LENGTH];

    char ** OutStr (char * In_Str, char delim);

    int ParsePairs(char * str, char Left_Char, char Right_Char);
    //return strlength if successful, 0 otherwise

    int Parse(char *str, char delim); //normal strings

    void main()
    {
    char *Test_Str = "\"asf,def\",<a,2>,3,{4,5}";
    char **test_output;

    test_output = OutStr(Test_Str, ',');
    cout << "done\n";
    return;
    }

    char **OutStr (char * In_Str, char delim)
    {
    //char *Temp[LIST_LENGTH];
    char In_Str_Copy[STRING_LENGTH];
    char Temp_Str[STRING_LENGTH];
    char * Str_Ptr;
    int i, j;

    //first make a copy of the ctring
    strcpy(In_Str_Copy, In_Str);
    //strcpy(Temp_Str, In_Str);
    Str_Ptr = In_Str_Copy;

    //allocate memory
    for (i=0; i<LIST_LENGTH; i++)
    {
    Temp[i] = new char[STRING_LENGTH];
    Temp[i][0] = 0;
    }

    //parsing string

    // null string
    if(!In_Str)
    return 0;

    // string without delimiters
    if(!strchr(In_Str_Copy, delim))

    {
    strcpy(Temp[0], In_Str_Copy);
    return Temp;
    }

    j = 0;
    int Normal_Flag = 0;
    while(1) //process string
    {
    switch (In_Str_Copy[0])
    {
    case '(':
    i = ParsePairs(In_Str_Copy, '(',')');
    break;

    case '<':
    i = ParsePairs(In_Str_Copy, '<','>');
    break;

    case '{':
    i = ParsePairs(In_Str_Copy, '{','}');
    break;

    case '[':
    i = ParsePairs(In_Str_Copy, '[',']');
    break;

    case '"':
    i = ParsePairs(In_Str_Copy, '"','"');
    break;

    default:
    i = Parse(In_Str_Copy, delim);
    Normal_Flag = 1;

    }

    //check In_Str_Copy[i], should be delim
    //to be added
    if(!Normal_Flag)
    strncpy(Temp[j], In_Str_Copy, i+1);
    else
    strncpy(Temp[j], In_Str_Copy, i);

    Normal_Flag = 0;

    j++;

    //check if this is the last character

    i++;

    //check delim
    if(!In_Str_Copy[i])
    return Temp;

    if(In_Str_Copy[i] == delim)
    {
    i++;
    }



    if(In_Str_Copy[i])
    {
    strcpy(Temp_Str, Str_Ptr + i);
    strcpy(In_Str_Copy, Temp_Str);
    Str_Ptr = In_Str_Copy;
    }
    else
    break;

    }

    return Temp;
    }

    int Parse(char *str, char delim)
    {
    int i = 0 ;

    while (str[i] && (str[i] != delim))
    i++;
    return i;


    }

    int ParsePairs(char * str, char Left_Char, char Right_Char) //parse strings within pairs
    {

    if(str[0] != Left_Char)
    return -1;

    // search right_char
    if(!strchr(str, Right_Char))
    return -1;

    int i = 1;

    while(str[i] != Right_Char)
    i++;

    //str[i] = 0;

    return i;
    }

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Just create a class that carries that info in a variable, and then return a variable of that type. I don't know if this is exactly what you want, but here's an example:

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    
    class str
    {
    char temp[1024];
    int len;
    
    public:
    
    str(char a[]="")
    	{
    	strcpy(temp,a);
    	len=strlen(temp);
    	}
    
    str operator+(str s1)
    	{
    	char b[1024];
    	strcpy(b,temp);
    	b[len]='\0';
    	strcat(b,s1);
    	str retval(b);
    	return retval;
    	}
    
    operator char*()
    	{
    	char retval[1024];
    	strcpy(retval,temp);
    	retval[len]='\0';
    	return retval;
    	}
    
    void display()
    	{
    	cout<<temp;
    	}
    
    void addspace()
    	{
    	temp[len]=' ';
    	len++;
    	temp[len]='\0';
    	}
    
    int operator==(str s1);
    	
    char* strreverse();
    
    
    };
    
    char* str::strreverse()
    	{
    	char n[1024];
    	int cpos=len-1;
    	int npos=0;
    
    	for( ; cpos>=0 ; cpos--)
    		{
    		n[npos]=temp[cpos];
    		npos++;
    		}
    
    	n[npos]='\0';
    	str retval(n);
    	return retval;
    	}
    
    
    int str::operator==(str s1)
    	{
    	int eq=0;
    	int slen=strlen(s1);
    
    	if (len>slen)
    		{
    		int t=0;
    		t=slen;
    		slen=len;
    		len=t;
    		}
    
    	if (len==slen)
    		{
    		eq=1;
    		slen--;
    		for ( ; slen>=0 ; slen--)
    			{
    			if (s1[slen]!=temp[slen])
    				{
    				eq=0;
    				break;
    				}
    
    			}
    		}
    
    	return eq;
    	}
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Probably the easiest way is just to pass the array as a parameter as the code below shows. If you want it as the return instead, you need to make it a **char and dynamically allocate both dimensions. I can post how to do that also. I also changed you strncpy()'s to strncat()'s. Seems to work better on my computer. Also, I freed up the memory in main().

    #include <iostream.h>
    #include <string.h>

    const int LIST_LENGTH = 10;
    const int STRING_LENGTH = 100;
    char CHAR_ARRAY[STRING_LENGTH];

    void OutStr(char * In_Str, char delim, char *Temp[]);

    int ParsePairs(char * str, char Left_Char, char Right_Char);
    //return strlength if successful, 0 otherwise

    int Parse(char *str, char delim); //normal strings

    int main()
    {
    char *Test_Str = "\"asf,def\",<a,2>,3,{4,5}";
    char *test_output[LIST_LENGTH];

    OutStr(Test_Str, ',', test_output);
    cout << test_output[0] << endl;
    cout << test_output[1] << endl;
    cout << test_output[2] << endl;
    cout << test_output[3] << endl;
    cout << test_output[4] << endl;
    cout << "done\n";
    for (int i=0; i<LIST_LENGTH; i++)
    {
    delete []test_output[i];
    }
    return 0;
    }

    void OutStr(char * In_Str, char delim, char *Temp[])
    {
    char In_Str_Copy[STRING_LENGTH];
    char Temp_Str[STRING_LENGTH];
    char * Str_Ptr;
    int i, j;

    //first make a copy of the ctring
    strcpy(In_Str_Copy, In_Str);
    //strcpy(Temp_Str, In_Str);
    Str_Ptr = In_Str_Copy;

    //allocate memory
    for (i=0; i<LIST_LENGTH; i++)
    {
    Temp[i] = new char[STRING_LENGTH];
    Temp[i][0] = 0;
    }

    //parsing string

    // null string
    if(!In_Str)
    return;

    // string without delimiters
    if(!strchr(In_Str_Copy, delim))

    {
    strcpy(Temp[0], In_Str_Copy);
    return;
    }

    j = 0;
    int Normal_Flag = 0;
    while(1) //process string
    {
    switch (In_Str_Copy[0])
    {
    case '(':
    i = ParsePairs(In_Str_Copy, '(',')');
    break;

    case '<':
    i = ParsePairs(In_Str_Copy, '<','>');
    break;

    case '{':
    i = ParsePairs(In_Str_Copy, '{','}');
    break;

    case '[':
    i = ParsePairs(In_Str_Copy, '[',']');
    break;

    case '"':
    i = ParsePairs(In_Str_Copy, '"','"');
    break;

    default:
    i = Parse(In_Str_Copy, delim);
    Normal_Flag = 1;

    }

    //check In_Str_Copy[i], should be delim
    //to be added
    //strcpy(Temp[j],"");
    if(!Normal_Flag)
    //strncpy(Temp[j], In_Str_Copy, i+1);
    strncat(Temp[j], In_Str_Copy, i+1);
    else
    //strncpy(Temp[j], In_Str_Copy, i);
    strncat(Temp[j], In_Str_Copy, i);

    Normal_Flag = 0;

    j++;

    //check if this is the last character

    i++;

    //check delim
    if(!In_Str_Copy[i])
    return;

    if(In_Str_Copy[i] == delim)
    {
    i++;
    }



    if(In_Str_Copy[i])
    {
    strcpy(Temp_Str, Str_Ptr + i);
    strcpy(In_Str_Copy, Temp_Str);
    Str_Ptr = In_Str_Copy;
    }
    else
    break;

    }

    return;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  2. Advice on multithreading
    By Calef13 in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2007, 04:28 PM
  3. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 07:38 PM
  4. need advice on project
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 07-23-2003, 02:06 PM
  5. Who's telling the truth??? Career Advice Needed Badly
    By Ican'tCjax,fl in forum C Programming
    Replies: 1
    Last Post: 11-06-2002, 06:16 PM

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