0

Can someone tell me what's wrong with my code? I've looked extensively at the error messages VS gives me, but I'm at a loss. I've attaches error messages for more info.

//Inclusion of necessary header files
#include <stdio.h>
#include <string.h>

//String Removal function prototype
char str_check(char *, char *);
void str_remove(char*, char*);
int isPrefix(char* list1, char* list2);
//Entry point for header files, pre-processor directives, and function prototypes
int main()
{
    //Necessary variable declarations
    char list1[500];
    char list2[20];

    //Input prompt and input function for first string
    printf("Please enter a string of up to 500 characters for first array.\n");
    *list1 = getchar();

    //Input prompt and input function for second string
    printf("Please enter a string of up to 20 characters for second array.\n");
    *list2 = getchar();

    //String Removal function call assigns function output to a third array and uses string function to display output
    *list1 = str_check(list1, list2);
    puts(list1);

    //Exit sequence
    return 0;
}

//String check function definition
char str_check(char* list1, char* list2)
{
    //Temporary character pointer declarations and for loop control variables
    char *ptr1=list1, *ptr2=list2;
    int i, j, k;

    //Loop checks both array elements for concurrent characters using isPrefix function, checks for concurrent string segments if one is found, call string removal function
    for (i = 0; i < strlen(list1); i++)
    {
        if (isPrefix(ptr1+i, ptr2)==1)
        {
            str_remove(ptr1+i, ptr2);
        }

    }

    return;
}

//isPrefix function definition checks for string occurences
int isPrefix_1 (char* ptr1, char* ptr2){

    int i=0;

    while(*(ptr1 + i) != '\0')
    {
        if(*(ptr1 + i) != *(ptr2 + i))
        {
            return 0;
        }
        i++;
    }

    return 1; 
}


void str_remove(char* ptr1, char* ptr2)
{
    int offset = strlen(ptr2);
    int i=0;
    for(i=0;i<(strlen(ptr1)-strlen(ptr2));i++)
    {
        *(ptr1+i) = *(ptr1+i+offset);
    }
    *(ptr1+i+offset) ='\0';
}

ERROR MESSAGES:
: warning C4018: '<' : signed/unsigned mismatch
: warning C4033: 'str_check' must return a value
: warning C4101: 'k' : unreferenced local variable
: warning C4101: 'j' : unreferenced local variable
: warning C4018: '<' : signed/unsigned mismatch
: warning C4716: 'str_check' : must return a value
: fatal error LNK1120: 1 unresolved externals
carlodurso
  • 2,886
  • 4
  • 24
  • 37
  • regarding this and similar lines: *list1 = getchar(); the getchar() function does NOT get a bunch of characters. Rather it only gets a single character. You might try using fgets() which can acquire a whole string, even setting the null char at the end of the input string. However, it stops (after consuming) a newline or encountering an EOF event. – user3629249 Nov 09 '14 at 01:33
  • this line: *list1 = str_check(list1, list2); will fail, as all it does in set the first few bytes of the list1 array with a pointer to something returned by str_check(). Probably NOT what you want to do. – user3629249 Nov 09 '14 at 01:35
  • all the 'error' messages, except the last, are compile time errors and need to be corrected to obtain a clean compile. The last 'error' message is output by the linker, and can be ignored since the compile had already failed. – user3629249 Nov 09 '14 at 01:38
  • every 'C' compiler that I have ever worked with outputs the file name and the line number (and sometimes) the column number of where the error/warning was found. Some IDEs are setup so clicking on the error message will move the editor to the location of the error/warning. even then, those error/warning messages contain the file line number as part of the message. What are you using for a compiler that skips outputing such basic info? – user3629249 Nov 09 '14 at 01:39
  • ptr2 (a terrible, uninformative name) can overrun the list2 array during certain loops, like in the isPrefix_1() function (which is not called anywhere, also isPrefix() function is called, but does not exist in the presented code. – user3629249 Nov 09 '14 at 01:51
  • I suggest fixing the compiler messages then fix the linker messages, then (and only then) start debugging the code. – user3629249 Nov 09 '14 at 01:53

1 Answers1

1
  1. warning C4018: '<' : signed/unsigned mismatch

I don't know exactly where on which line is this warning, but I assume it is here:

for (i = 0; i < strlen(list1); i++)

strlen returns a size_t variable, which is an unsigned int. You're comparing it to a signed variable, and this might create you some troubles. For example, considering the following declarations:

int a = -1;
unsigned int b = 100;

The comparison a < b will be false. This is because a is casted to unsigned and due to the way negative numbers are represented (two's complement, usually). However, in your case this will not be a problem. To avoid getting the warning, declare i as unsigned int.

  1. warning C4033: 'str_check' must return a value

The declaration of str_check is:

char str_check(char* list1, char* list2);

However, you don't return any char in the definition of this function. Either you should have declared it void str_check(char* list1, char* list2);, or you should have returned some value from it.

  1. warning C4101: 'k' : unreferenced local variable

This one is simple: you have declared a variable k and you don't use it.


The rest of them are duplicated of the above (except the last one). Excepting the second warning, which might cause you some unexpected behavior, the program should run flawlessly.

The last error is your problem. I will try to give you some background about the meaning of the error and some references that I hope will help you.

You have a source code. In order to create an executable file from it, there are some steps which must be taken:

  1. Preprocessing.

In this step the preprocessor directives are analysed. In your case, you include some file using the #include directive. When the preprocessor encounters this directive, it simply copies the content of that file into yours.

  1. Compilation.

This step produces the actual object-code for your program (the one that the CPU understands). However, there are some functions, like getchar, for which you have not provided any definition. They are already compiled in libraries.

  1. Linking.

The job of the linker is to link your object code generated in the last step with the required libraries, so all the functions you use can be called. This step generates your error. It basically says that it cannot find a reference to something. Here you will find some documentation for this error:

"1 unresolved externals" C++ http://support.microsoft.com/kb/815650 http://www.dreamincode.net/forums/topic/22701-fatal-error-lnk1120-1-unresolved-externals/

Community
  • 1
  • 1
Paul92
  • 8,827
  • 1
  • 23
  • 37
  • I appreciate the help, but I still don't understand why isPrefix is an unresolved external, the program should be able to connect the function definition to the function call. – sourcecodedysfunction Nov 10 '14 at 20:12
  • Well...you haven't defined it (at least not in the code you posted) and it isn't in the standard libraries that you include. – Paul92 Nov 10 '14 at 22:01