Thread: Just Can't Get This Code to Work

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    9

    Just Can't Get This Code to Work

    Hello everyone!

    I'm new around here, but that's pretty obvious. So I'll your time by skipping the introduction and getting right to the code.

    I decided to start learning C the other day. Now, I'm new to C itself, but not its syntax; I've done quite a bit of programming in a few of C's derived languages.

    After a couple "Hello, world" type programs, I decided to write a BASIC interpreter in C as my first real project (and yes, I know what I'm getting in to... ;-) ) It's called Portable BASIC, and a stable version of the source is available here. (Compiles with TCC on Android.)

    Anyways, I've begun work on implementing the print statement. What I'm having trouble with is getting the string to be printed.

    Portable BASIC is a line-by-line interpreter, and it reads the instructions out of an input string.

    I'm trying to use this code to retrieve the print string, but I keep getting a Segmentation Fault on the second printf.

    Code:
          printf("%i", strlen(comStr));
          string check;
          for(i=0;i <= strlen(comStr) -6; i++)
          {
              check[i] = comStr[i+5];
          }
          printf("%s", check);
    String is defined as a type earlier in my program, as an array of chars. comStr is the command line received from the interpreter.

    I want check to display the rest of comStr after the first quotation mark.

    Any ideas?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're probably not null terminating check. After the for loop, try adding the line
    Code:
    check[i] = '\0';

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You're not terminating the string check with a zero terminator.

    Anyway, you can print starting with the 6th character from comStr directly:
    Code:
    printf("%s", comStr + 5);

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Is 'check' defined as a byte array? I can't tell from the keyword 'string'. It's not native C.
    Also, it could be the console that displays the output is buffering. Perhaps you need to put a newline character: printf("%s\n", check);

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    String is defined as a type earlier in my program, as an array of chars.
    You need to be more specific. An array of how many chars? If an array, is it long enough to hold the data (including the zero terminator that others have mentioned) you are placing in it.

    If you have made the common mistake of believing a pointer is an array, you need to correct that. A pointer is not an array.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    qny, I tried your suggestion, but nothing printed. So I changed it to comStr + 2 and got this.
    Code:
    Portable BASIC v1.36, rev10
    Copyright (C) Waterfront Software 2012    
                      
    Approx. 2130575361 bytes free to C
    >print "hello"                                              
    ello"
    >
    nonoob, string is defined as a character array, just to make the transition to C easier for me.
    Code:
    typedef char * string;

    Is there any easier way to get a substring in C?

    Edit: Ah, I defined string as a pointer. How should I define it properly?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Honestly, I wouldn't define a type called string, for exactly that reason. It masks what it is you're trying to do. You had a pointer, pointing a garbage address, that you were trying to copy into, hence the crash. You could typedef string as a char array, but then you have to give it a fixed size, which may be undesirable. I would just declare a char array of a suitable size. It's a very well understood idiom in C, and avoids the "what is a string type, really" question.

    It looks like comStr doesn't contain the keyword "print", so you don't need the + 5. If you know from your tokenizing routine, that comStr will only be the quotes and stuff inside it, you can set the last char to null and print starting at comStr+1.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    Well, comStr is the entire input line, as in:
    Code:
    print "hello"
    I just can't find a good way to get a substring of hello.

    (Oh, and by the way, after this experience I'm probably not going to define a string type in future programs...)
    Last edited by WMH; 09-14-2012 at 04:13 PM.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes, you need to have defined a memory area... either by defining char check[50], or by just keeping it a pointer but using malloc to give it a valid address into a suitably large array.
    Substring example:
    strcpy(check, comStr + 5);

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    OK, here's my new code.
    Code:
          char check[65536];
          strcpy(check, comStr + 5);
          printf("%s", check);
    And here's the output it gives.
    Code:
    Portable BASIC v1.36, rev10
    Copyright (C) Waterfront Software 2012
                          
    Approx. 2130575361 bytes free to C
    >print "hello"                                              
    >print "jrjtjfjrjtktktktktkkiytktjtjt"
    tktktkkiytktjtjt"
    >
    What's the deal? Obviously C is swimming in memory, so that can't be it...

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by WMH View Post
    I just can't find a good way to get a substring of hello.

    Code:
    char *q1 = strchr(comStr, '"'); // if not NULL, q1 points to the first quote
    if (q1) {
        char *q2 = strchr(q1 + 1, '"'); // if not NULL, q2 points to the second quote
        if (q2) {
            printf("%.*s", (int)(q2 - q1 - 1), q1 + 1); // print whatever is between the quotes
        } else {
            /* maybe some error */
        }
    } else {
        /* maybe some error */
    }
    Last edited by qny; 09-14-2012 at 04:23 PM.

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    Thanks so much, qny!

    Just a quick question: could I change the printf to this to store the string in a variable?

    Code:
    char * prStr = "%.*s", q2-q1-1, q1 + 1

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by WMH View Post
    What's the deal? Obviously C is swimming in memory, so that can't be it...
    You haven't provided enough information. Show us a SMALL and COMPLETE example of code that illustrates your problem.

    All you are doing at present is showing code snippets, and output that produced by some other means. You might know how the bits fit together relative to your code. But forum members are not mindreaders.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by WMH View Post
    Thanks so much, qny!


    Just a quick question: could I change the printf to this to store the string in a variable?


    Code:
    char * prStr = "%.*s", q2-q1-1, q1 + 1

    Not like that you can't, look into snprintf.

  15. #15
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by WMH View Post
    Just a quick question: could I change the printf to this to store the string in a variable?

    Code:
    char * prStr = "%.*s", q2-q1-1, q1 + 1
    No.

    If you don't want to change the original string you need to copy it (using strncpy and properly terminating the result).
    Code:
    strncpy(copy, q1 + 1, q2 - q1 - 1);
    copy[q2 - q1 - 1] = '\0';
    If you can change the original and don't mind doing it, it may be easier to replace the 2nd quote with a NUL (a '\0').
    Code:
    *q2 = 0; /* replace second quote with NUL */
    printf("%s", q1 + 1); /* print from just after the first quote to where the 2nd quote used to be */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I work out the AWT and TAT in my code ?
    By spendotw in forum C Programming
    Replies: 0
    Last Post: 01-21-2012, 07:51 AM
  2. Why does this code work?
    By Whitewings in forum C Programming
    Replies: 8
    Last Post: 08-16-2011, 12:58 PM
  3. this code won't work
    By med linux in forum C Programming
    Replies: 7
    Last Post: 03-23-2011, 08:35 AM
  4. My Code Does Not Work? Why?
    By mintsmike in forum C Programming
    Replies: 8
    Last Post: 03-24-2009, 02:03 PM
  5. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM