Thread: I'm trying to understand why this string is being weird.

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    8

    I'm trying to understand why this string is being weird.

    So, in an effort to solve this problem, I'm trying to get how Strings work in relation to pointers.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    #define MAX 1000
    
    
    int main()
    {
      char *str1, *str2;
      char msg1[MAX], msg2[MAX];
      char* loc;
    
    
    
    
      str1 = msg1;
      str2 = msg2;
      printf("Please enter the first string.");
      scanf("%s", msg1);
      printf("Good, now please enter the second string.");
      scanf("%s", msg2);
      int count = 0;
    
    
    for( str1 != '\0' ;; str1++)
    {
      printf("[%s]", str1);
    
    
      if(str1 == '\0')
        break;
    
    
      else if((loc = strstr(str1, str2)) != NULL)
      {
        str1 += sizeof(str2);
        if(str1 == "\n")
          count++;
        printf("\nthe string %s is on line %d",loc, count);
      }
    
    
      else if(str1 == '\n')
        count++;
    
    
    
    
    }
    
    
    }
    So, I'm trying to start small and make my program effectively return the line of a string.

    that second string is returned when the strstr() method isn't null and when strstr() isn't null, then my program should iterate further in the first string according the the value of the second string and keep iterating.

    So if I type Matt\nBooty\nMattBooty\nMatt\nBooty in string a and type Booty in string B, it should tell me which line Booty appears on

    since my code should iterate every time \n is found in the string. However, it seems that my code doesn't adhere to those principles, because even though I made it so that when \n appears in the string that count would increase, it doesn't seem to do anything.

    Furthermore, the string doesn't seem to have '\0' or a null termination character at the end.

    What my code ends up showing is this: the string BootyMattBootyMattBootyMattBooty is on line 0[ootyMattBootyMattBootyM - Pastebin.com

    What did I do wrong here?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You've forgotten that %s with scanf reads a string until whitespace, and that includes a newline.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    8

    Understood.

    Alright, that explains why \n isn't registering, but that doesn't explain all of the other weirdness.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    It looks like you misunderstand how to use pointers. str1 is a pointer to char. *str1 is a char. You can't meaningfully compare a pointer to char (like str1) with a char (like '\0' or '\n').

    Also, in your own words, what do you think this line is supposed to do?

    Code:
    for( str1 != '\0' ;; str1++)

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    8
    That's where I'm confused, a string is just an array of chars, right? And a pointer is effectively the same as an array, right? So, why can't I compare a pointer to a char in this context?

    Well, when I wrote that I had the intention of basically iterating through the pointer and making it go until str reached it's terminating null character and it would keep iterating until doing so.

    Problem is, it just goes and goes until the array reaches the end and fills the array with a bunch of junk characters.

    I REALLY don't get why that's happening. I've written new code since then obviously, but I'd like to get why this doesn't work out.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Well, as I mentioned, *str1 is a char, so you can compare it against another char like '\0'. For example:

    Code:
    if (*str1 == '\n') {
        // ...
    }
    The * in front of str1 dereferences the pointer, which means that it "gets" whatever str1 points to rather than the pointer str1 itself.

  7. #7
    Registered User
    Join Date
    Jun 2019
    Posts
    8
    Okay, so to compare an item to the pointer's content I have to de-reference it and tell the machine that "hey, make that pointer a char again", right? Kind of like doing str[i] == '\n' if I was running a for loop??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me understand string
    By princez90 in forum C Programming
    Replies: 1
    Last Post: 04-07-2008, 03:37 AM
  2. Weird string problem
    By kidburla in forum C Programming
    Replies: 13
    Last Post: 08-09-2006, 03:14 AM
  3. A really weird problem. I understand the error, but not a clue why I get the errors.
    By Finchie_88 in forum Networking/Device Communication
    Replies: 1
    Last Post: 02-20-2005, 09:48 AM
  4. Weird string output only for first run
    By Evilelmo in forum C++ Programming
    Replies: 4
    Last Post: 03-04-2003, 09:00 PM
  5. some really weird problems with string !
    By mellisa in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2003, 04:56 AM

Tags for this Thread