Home > database >  C external errors and 1 warning
C external errors and 1 warning

Time:01-22

I am getting these errors when trying to compile, and the only thing underlined is strcpy_s(name, strlen(input) 1, input); Which is the warning. This is part of an assignment and part of the instructions was to make both a Topic.c (the code below) and a Topic.h but no instructions on what to put in the .h file. I have never seen these errors before and have researched them but I cannot find references relevant to this issue. This is not a fix code assignment this should be ready to run I am only supposed to understand the concepts. Not sure what question to ask. I have checked several times to ensure the code was typed in correctly.

LNK2019 unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

and

LNK1120 1 unresolved externals

and 1 warning

C6387 'name' could be '0': this does not adhere to the specification for the function 'strcpy_s'.

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

   /*
   * Swap 1 method with scalars: why does this not work?
   */
   void swap1(int x, int y)
   {
       int temp = x;
       x = y;
       y = temp;
   }

   /*
   * swap 2 method with pointers: why does this work?
   */
   void swap2(int* x, int* y)
   {
       int temp = *x;
       *x = *y;
       *y = temp;
   }

   /*
   * Play Pointer method: Don't forget to clean up your malloc!
   */
   char* playPointer(char* input)
   {
       // Allocate memory to hold a copy of the input array, copy input to it, and 
          return a pointer to a new array
       // What is the advantage of assigning address of a variable to a pointer 
          variable?
       // Why does swap1() not work, and why does swap2() work?
       // How does strpy_s() make code more secure?
       // How does strpy_s() demonstrate defensive coding?
       // Look up strncpy() and compare it to strpy_s() How are they similar?

       char* name = malloc(strlen(input) 1);
       strcpy_s(name, strlen(input) 1, input);
       return name;
   }

CodePudding user response:

There is no main function in your code.

The main function in the C language is the program entry point and it has to be present to run the compiled program.

In your case the linker cant find it and reports the error.

CodePudding user response:

If this is supposed to be a standalone program, rather than something linked into other programs, it needs a main() function.

To suppress the warning about name, wrap the code that calls strcpy_s() in an if statement that checks that malloc() succeeded.

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

/*
 * Swap 1 method with scalars: why does this not work?
 */
void swap1(int x, int y)
{
    int temp = x;
    x = y;
    y = temp;
}

/*
 * swap 2 method with pointers: why does this work?
 */
void swap2(int* x, int* y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}

/*
 * Play Pointer method: Don't forget to clean up your malloc!
 */
char* playPointer(char* input)
{
    // Allocate memory to hold a copy of the input array, copy input to it, and return a pointer to a new array
    // What is the advantage of assigning address of a variable to a pointer variable?
    // Why does swap1() not work, and why does swap2() work?
    // How does strpy_s() make code more secure?
    // How does strpy_s() demonstrate defensive coding?
    // Look up strncpy() and compare it to strpy_s() How are they similar?

    char* name = malloc(strlen(input) 1);
    if (name != NULL) {
        strcpy_s(name, strlen(input) 1, input);
    }
    return name;
}

int main(void) {
    return 0;
}
  •  Tags:  
  • Related