Thread: Seg fault out of no where

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Seg fault out of no where

    for some reason this gives me a seg fault, i cant see why. all my pointers are assigned to valid vars or NULL.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	char temp[] = "test<abc>data</cba>test";
    	remove_tag(temp);
    	
    	return 0;
    }
    
    int remove_tag(char *data) {
    	char value[255];
    		strcpy(value, data);
    	int x = 0;
    
    	int y = 0;
    	
    	char *start = strchr(data, '<'); /* Find the start of a tag. */
    	char *end = strchr(data, '>');   /* Find the end of a tag. */
    		end = strchr(data, '>');
    
    	while(x < *start) { /* Copy everything before the tag. */
    		data[x] = value[y];
    
    		++x;
    		++y;
    	}
    
    	x = *end + 1;
    
    	while(data[x] != '\0') { /* Copy everything after the tag*/
    		data[x] = value[y];
    
    		++x;
    		++y;
    	}	
    
    	strcpy(data, value);
    	
    	return 0;		
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    while(x < *start) { /* Copy everything before the tag. */

    This is probably not what you think. You pass it a character array, which you dereference character by character. You are comparing the ascii value stored in said character with the integral value of x.

    This loop will continue until hit hits a null, or some other character such as a new line, or something, whose ascii value is less than your counter.

    I really doubt that this is what you want.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    with while(x < *start), is there a way i can use start as a subscript? i mean like data[start].

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char *end = strchr(data, '>'); /* Find the end of a tag. */
    end = strchr(data, '>');

    These lines also do the exact same thing. Remove the second one.

    I'm not sure I understand exactly wat you're trying to do. Basicly, I'd do this:

    ptr1 = strchr( data, '<' );
    ptr2 = strchr( ptr1, '>' );

    This will make sure that the second pointer comes after the first in the list. Naturally, you will actually want to do error checking before you do the second call.

    Anyway, then you just do this:

    strncpy( buffer, ptr1+1, (ptr2-ptr1)-1 );

    That should be all you need. If my reasoning is correct, this is what should happen:

    ptr1 points to the <
    ptr1+1 points to whatever is directly past the <
    ptr2 - ptr1 - 1 is the length of the string excluding the > and the <.

    That should suffice for copying the contents between the < and the > into a buffer.

    Is that what you're trying to do?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Is that what you're trying to do?
    nope. if i run what is in main(with char temp[] = "test<abc>data</cba>test") i want to get back "testtest". i want to just delete the html tags.

    since this is supposed to work on real html tags, i need that 2nd end = strchr(data, '>');. since its <tag>some_value</tag> i want to get the 2nd > so i can end it. that seems alittle weird calling the same function 2 times, any better ideas?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your best bet is actually to write a recursive function. This will resolve problems with nested tags. I honestly don't see the need to strip out HTML tag enclosures. Consider the following:
    Code:
    {html}
    {head}
        Some text goes here.
    {/head}
    {body}
        {p}
            Some test here.
        {/p}
    {/body}
    {/html}
    Your code does not allow for this scenario. What you should be doing is probably using a recursive function, that, when you encounter a <, it calls itself. When it encounters a > it returns. Or something similar.

    Furthermore, what would your function expect to return in this scenario? It should according to your outline, return nothing, which is basicly what will happen any time you ever pass your function an HTML file.

    Note: I used {} rather than <> so the forum wouldn't eat my HTML.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM