1

The following piece of code is scanning for command line arguments for valid sentences that contains the words 'Int' or 'Float'.

I have debugged it for hours trying to locate where the error is occurring to no avail.

Any help would be appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

bool process_input(int, char *[]);
bool first_word_correct(char *);
void print_string(char *);

int main(int argc, char *argv[]) {
    if (!process_input(argc, argv))
        printf("Incorrect input.\n");
    else
        printf("CORRECT input.\n");

    return EXIT_SUCCESS;
}

bool process_input(int argc, char *argv[]) {
    char *word;
    int i, j;
    bool is_new_sentence = true;

    i = 0;
    j = 0;

    while (++i < argc) {
        // check the first word - must be "A" or "An"

        if (is_new_sentence) {            
            if (!first_word_correct(argv[i])) {
                return false;
            }

            is_new_sentence = false;
        }

        // we're checking subsequent words of the same sentence

        else if (!is_new_sentence) {
            // is this the last word?

            strcpy(word, argv[i]);

            if (word[strlen(word) - 1] == '.') {
                is_new_sentence = true;
            }

            switch(is_variable_name(word)) {
                case 1:
                    printf("int found!\n");
                    break;
                case 2:
                    printf("float found!\n");
                    break;
            }
        }
    }

    return true;
}

bool first_word_correct(char *word) {    
    if (strcmp(word, "A") == 0 || strcmp(word, "An") == 0) {
        return true;
    }
    else {
        return false;
    }
}

/*
 *int 1
 *float 2
 */

int is_variable_name(char *word) {
    if ((strcmp(word, "int") == 0) ||  (strcmp(word, "int.") == 0))
        return 1;

    if ((strcmp(word, "float") == 0) ||  (strcmp(word, "float.") == 0))
        return 2;

    return -1;
}
6
  • 1
    you need to allocate memory for word before you strcpy(word, argv[i])... or you could avoid the copy if you are not going to modify the word. Commented Oct 29, 2014 at 4:07
  • If you're using a flavour of UNIX, you could use GDB. Commented Oct 29, 2014 at 4:14
  • If I am not modifying the word, what should I use to copy the string? Commented Oct 29, 2014 at 4:17
  • If you're using strcpy, it creates a new string, and so you cannot send a dangling pointer Commented Oct 29, 2014 at 4:20
  • 1
    @DamienLowe as you are not modifying the input, you can just use: char* word = argv[i]; Commented Oct 29, 2014 at 9:37

1 Answer 1

3

You are not allocated memory for word before using it. Try like

       else if (!is_new_sentence) {
        // is this the last word?
        word=malloc(100);// size is based on your argv[i]
        strcpy(word, argv[i]);

        if (word[strlen(word) - 1] == '.') {
            is_new_sentence = true;
        }
Sign up to request clarification or add additional context in comments.

8 Comments

@P0W ofcourse i wont forget. Just for giving an idea i have written.
That was for OP, never mind, it always a good practice to write that in answer, if OP is unaware of it, s(he) may end up leaking resources
I added word=malloc(sizeof(argv[i]), and now I am getting an error indicating an 'invalid pointer' and then aborting.
@DamienLowe sizeof(argv[i]) will give you size of pointer, not the length of argv[i]
@POW word=malloc(strlen(argv[i])+1);// 1 is for NULL
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.