Thread: String and character pointers

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    String and character pointers

    Ok im brand new to c . Been programing in delphi and stuff for a while but at the moment im just using nedit and cc in linux.

    Im trying to write a program to read a file, and print out the links it has. Most of the code i have tested and works, the problem is the "p[0] = &a;" line. The idea behind it is, i want to shift the first character in the string up some after ive found a link, so it caries on from where it left off, rather than starting from the start and finding the same link each time.

    Googled for a while but im totaly stumped this time!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void readfile(FILE *fp, char *p);
    void findtag(char *p, char *start, char finish, char *result);
    
    main(int argc, char *argv[])
    {
    	FILE *fp;
    	char c[10000], tag[100];
        
    	if ((argc != 1) && ((fp = fopen(*++argv, "r")) != NULL))
    	{
            	readfile(fp,c);
            	fclose(fp);
    	
    		while(c != '\0')
    		{
    			findtag(c,"<a",'>',tag);
    			printf("%s\n",tag);
    			/* findtag(tag,"href=",' ',tag); */
    		} 
    	}
    	else
    		printf("Arf\n");
    
    }
    
    void readfile(FILE *fp, char *p)
    {
    	int i;
    
    	i = 0;
    	while ((p[i++] = getc(fp)) != EOF)
            	;
    	p[i - 1] = '\0';
    }
    
    void findtag(char *p, char *start, char finish, char *result)
    {
    	char *a;
    	int i;
    	
    	i = 0;
    	a = strstr(p, start) + strlen(start);
    	if (a != '\0')
    	{
    		while ((result[i++] = *a++) != finish)
    			;
    		result[i-1] = '\0';
    	}
    	
    	p[0] = &a;
    }

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    127
    Why not set a pointer to the beginning of the array in main, then pass a pointer to that pointer to findtag. That way you can update the pointer yet still use it in main for your loop test.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void readfile(FILE *fp, char *p);
    void findtag(char **p, char *start, char finish, char *result);
    
    main(int argc, char *argv[])
    {
      FILE *fp;
      char c[10000], tag[100];
      char *p;
    
      if ((argc != 1) && ((fp = fopen(*++argv, "r")) != NULL))
      {
        readfile(fp,c);
        fclose(fp);
    
        p = c;
        while(*p != '\0')
        {
          findtag(&p,"<a",'>',tag);
          printf("%s\n",tag);
          /* findtag(tag,"href=",' ',tag); */
        } 
      }
      else
        printf("Arf\n");
    
    }
    
    void readfile(FILE *fp, char *p)
    {
      int i;
    
      i = 0;
      while ((p[i++] = getc(fp)) != EOF)
        ;
      p[i - 1] = '\0';
    }
    
    void findtag(char **p, char *start, char finish, char *result)
    {
      char *a;
      int i;
    
      i = 0;
      a = strstr(*p, start) + strlen(start);
      if (a != '\0')
      {
        while ((result[i++] = *a++) != finish)
          ;
        result[i-1] = '\0';
      }
    
      *p = a;
    }
    Please note that I have not checked this thoroughly.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    114
    Ive given that a go, it now segmentation faults. (after it lists the links) so its basicaly working.

    I dont see why it segmentation faults because pointers still confuse me and i dont understand the thing you have done with **p and things. why two *'s?
    Last edited by kzar; 05-14-2004 at 10:09 AM.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    114
    Ok ive looked at it for ages, improved the code and tried sticking printf's everywhere. I still cant see why it segfaults at the end!

    Also now ive tried to add code to find the "'s around links and remove them but that doesnt work either!

    I hope c gets easier, im really banging my head againts a wall at the moment.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>
    
    void readfile(FILE *fp, char *p);
    bool findtag(char **p, char *start, char finish, char *result);
    void cleanup(char *tag);
    
    main(int argc, char *argv[])
    {
    	FILE *fp;
    	char c[10000], tag[100];
    	char *p, *o;
    
    	if ((argc != 1) && ((fp = fopen(*++argv, "r")) != NULL))
    	{
    		readfile(fp,c);
    		fclose(fp);
    
    		p = c;
    		while(findtag(&p,"<a",'>',tag))
    		{
    			o = tag;
    			findtag(&o,"href=",' ',tag);
    			cleanup(tag);
    			printf("%s\n",tag);
    		} 	
    	}
      	else
        		printf("Arf\n");
    
    }
    
    void readfile(FILE *fp, char *p)
    {
    	int i;
    
    	i = 0;
    	while ((p[i++] = getc(fp)) != EOF)
    		;
    	p[i - 1] = '\0';
    }
    
    bool findtag(char **p, char *start, char finish, char *result)
    {
    	char *a;
    	int i;
    
    	i = 0;
      
    	if (a = strstr(*p, start) + strlen(start))
    	{
    		while ((result[i++] = *a++) != finish)
    			;
    		result[i-1] = '\0';
    		*p = a;
    		return 1;
    	}
    	else
    		return 0;
    }
    
    void cleanup(char *tag)
    {
    	char *a, temp[100];
    	int i;
    	
    	if (tag[0] == '"')
    	{
    		i=1;
    		*a=tag[1];
    		while((*a++ = tag[i++]) != '"')
    			temp[i-1] = *a;
    		tag = temp;
    	}
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    bool findtag(char **p, char *start, char finish, char *result)
    {
    	/* ... */
    	if (a = strstr(*p, start) + strlen(start))
    [Warning 613] Possible use of null pointer 'unknown-name' in left argument to operator 'ptr+int'
    [edit]
    Did you mean to do something like this?
    Code:
       a = strstr(*p, start);
       if ( a != NULL )
       {
          a += strlen(start);
    Also,
    Code:
    void cleanup(char *tag)
    {
    	/* ... */
    		*a=tag[1];
    [Warning 530] Symbol 'a' not initialized
    [/edit]
    Last edited by Dave_Sinkula; 05-14-2004 at 02:17 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    114
    Code:
       a = strstr(*p, start);
       if ( a != NULL )
       {
          a += strlen(start);
    Looks good , i tried somthing similar except i did "a = a + strlen(start)" which gave me an error. That was the final straw and i posted here

    Could you explain why you used a += ... instead of a = a + ... ? What is += ?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    a += 10;
    is shorthand for
    a = a + 10;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    114
    ok ive just tried your sugestion with the += and it does what it did when i tried it by doing a = a + ... , it segmentation faults.

    i dont really understand the warning you posted about the cleanup function either .
    Last edited by kzar; 05-14-2004 at 03:45 PM.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Could you explain why you used a += ... instead of a = a + ... ? What is += ?
    A compound assignment of the form E1 op= E2 differs from the simple assignment expression E1 = E1 op (E2) only in that the lvalue E1 is evaluated only once.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    114
    What did you get that warning from by the way? im doing cc in linux to compile but if theres somthing else i could use that gave me more of a clue whats wrong with my programs i might give it a go.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >i dont really understand the warning you posted about the cleanup function either

    'a' is a pointer. It is not pointing anywhere. Later on, you do an assignment to where 'a' points ('*a'). So, you are dereferencing some random memory location. Bad -- could cause a seg fault.

    >ok ive just tried your sugestion with the += and it does what it did

    The += stuff was not the main issue. The main thing was that in 'findtag' the return value of strstr should be checked to see that is was not NULL before attempting to find an offset. Dereferencing a null pointer could cause a seg fault.

    >What did you get that warning from by the way?

    PC-lint for C/C++ (NT) Ver. 8.00m


    [edit]Boy am I typing slow today.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    114
    if ( a != NULL ) checks if its nulll doesnt it?

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >if ( a != NULL ) checks if its nulll doesnt it?

    Yup.

    Okay, how about this one.
    Code:
    void readfile(FILE *fp, char *p)
    {
    	int i;
    
    	i = 0;
    	while ((p[i++] = getc(fp)) != EOF)
    [Info 734] Loss of precision (assignment) (31 bits to 7 bits)
    		;
    	p[i - 1] = '\0';
    }
    FAQ
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>while ((p[i++] = getc(fp)) != EOF)
    buffer overflow anyone?

    Protect your buffers at all times, to keep your app safe.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    May 2004
    Posts
    114
    thanks for all the help guys , i looked closely at the code and saw you where right, i forgot to say where the pointer was actauly pointing to. I fixed that and it no longer segmentation faults.

    I didnt really understand why that would be a buffer overflow though, and the cleanup function still isnt working.

    Heres the code so far

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>
    
    void readfile(FILE *fp, char *p);
    bool findtag(char **p, char *start, char finish, char *result);
    void cleanup(char *tag);
    
    main(int argc, char *argv[])
    {
    	FILE *fp;
    	char c[10000], tag[100];
    	char *p, *o;
    
    	if ((argc != 1) && ((fp = fopen(*++argv, "r")) != NULL))
    	{
    		readfile(fp,c);
    		fclose(fp);
    
    		p = c;
    		while(findtag(&p,"<a",'>',tag))
    		{
    			o = tag;
    			findtag(&o,"href=",' ',tag);
    			cleanup(tag);
    			printf("%s\n",tag);
    		} 	
    	}
      	else
        		printf("Arf\n");
    
    }
    
    void readfile(FILE *fp, char *p)
    {
    	int i;
    
    	i = 0;
    	while ((p[i++] = getc(fp)) != EOF)
    		;
    	p[i - 1] = '\0';
    }
    
    bool findtag(char **p, char *start, char finish, char *result)
    {
    	char *a;
    	int i;
    
    	i = 0;
      	
    	a = strstr(*p, start);
    	if (a != NULL)
    	{
    		a += strlen(start);
    		while ((result[i++] = *a++) != finish)
    			;
    		result[i-1] = '\0';
    		*p = a;
    		return 1;
    	}
    	else
    		return 0;
    }
    
    void cleanup(char *tag)
    {
    	char *a, temp[100];
    	int i;
    	
    	if (tag[0] == '"')
    	{
    		i=1;
    		a=&tag[1];
    		while((*a++ = tag[i++]) != '"')
    			temp[i-1] = *a;
    		tag = temp;
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM