Thread: a reverse function

  1. #16
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Also I again missed the ==! But despite that the code fails!

  2. #17
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    I got the problem but don't get how to fix it!
    Since I use:

    Code:
    for (i=0;i<MAX_IP_DATA_ROWS;i++)
    		 {
    		   i_data[i] = (char *)malloc(sizeof(data[i]));
    		   i_data[i]=data[i];
    		 }
    when I change anything in i_data the same change is copied in data too!Can do something so that only values are copied?

    Thanks,
    Angkar

  3. #18
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    sigh....did you ignore my comment that you shouldn't typecast malloc?
    do val = malloc(sizeof(data[end]) * sizeof(char )); instead..

    char * rev_sign(char *ip)
    ? your function returns a pointer? no....
    char rev_sign(char *ip)

    you're not returning anything. you're passing the address of your array to ip. so when you do something to ip, you're actually doing it to your original string. the changes are kind of "passed on".

    why are you +ing start and -ing end?
    why dont you post your COMPLETE code..? that would make help much easier to give.



    one last thing , there's an "edit" button on the posts for a reason :P USE IT instead of making more and more posts, lol its a waste of space.
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #19
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Here it is:

    Code:
    void invert(char *data[],int p1,int p2)
    {
    	//printf("\n Inversion");
    	int i,j=0;
    	int diff = abs(p2-p1);
    	char *val;
    	char *i_data[MAX_IP_DATA_ROWS];
    	for (i=0;i<MAX_IP_DATA_ROWS;i++)
    		 {
    		   i_data[i] = (char *)malloc(sizeof(data[i]));
    		   i_data[i]=data[i];
    		 }
    
    
    	//printf("Hi%s\n",data[0]);
    
    	if (diff==1)
    		{
    			val = rev_sign(i_data[p1]);
    			strcpy(i_data[p1],val);
    		}
    	
    	else
    	{
    		int start;
    		int end;
    		for (start = p1,end = p2-1;start<=end;start++,end--)
    		{
    			val = rev_sign(data[start]);
    			strcpy(i_data[end],val);
    			val = rev_sign(data[end]);
    			strcpy(i_data[start],val);
    
    		}
    
    		
    	}
    
    	while(j<MAX_IP_DATA_ROWS)
    	{
    		puts(i_data[j]);
    		j++;
    	}
    	
    			
    }
    
    char * rev_sign(char *ip)
    {
    	if(ip[0]=='+')
    		ip[0]='-';
    	else
    		ip[0]='+';
    	return ip;
    }
    Again, I'm too weak in pointers. Please help me rectify my errors!

  5. #20
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    where is your main function??? is that your COMPLETE source?? how are you calling your invert function? or getting data to pass to your invert function? or anything? lol when i said to post your complete code, i wasn't like..pulling your leg or meaning your complete code for the functions. i figured complete meant all of it, lol
    Registered Linux User #380033. Be counted: http://counter.li.org

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you are so weak in pointers, why do you bother trying to make an array of strings? Why must you have an array of strings anyway?

    Again my advice is to simplify your life. Start the program over and just use strings. Once the program works the way you want, you now have a firm grasp of the logic.

    Then if you simply have to you can get it to work with arrays of strings if you really want to. At least with this design method in mind you can't totally ruin the program's logic while your doing something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM