-1

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;
   }
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    You're missing the `main()` function. If this is not the main program, use the `-c` option when compiling to just create a `.o` file, not try to link it. – Barmar Jan 21 '22 at 23:34
  • 1
    The warning is because you didn't check whether `malloc()` succeeded. – Barmar Jan 21 '22 at 23:35
  • Why do you have `'''` before and after the code? If you're trying to mark a code block, it should be 3 backticks, not quotes, and they should be on the line BEFORE the code. But since you've indented the code, you don't need that marker. – Barmar Jan 21 '22 at 23:36
  • Not sure how to check if malloc succeeded. Tried adding the main do I need int main()? Thanks for the tip on submitting code – user17997828 Jan 21 '22 at 23:49
  • `if (name != NULL)` – Barmar Jan 21 '22 at 23:50
  • I still get the same errors and also tried if (name != notNULL) just in case – user17997828 Jan 22 '22 at 00:05

2 Answers2

0

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.

0___________
  • 60,014
  • 4
  • 34
  • 74
0

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;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ill try that thanks – user17997828 Jan 22 '22 at 00:20
  • This it generated an output with nothing but code 0 but the warning remains about name could be 0. C6387 'name' could be '0': this does not adhere to the specification for the function 'strcpy_s'. Should I add delete name; ? I read that somewhere, if so how do I define delete? – user17997828 Jan 22 '22 at 00:30
  • What output are you expected? The program doesn't do anything. – Barmar Jan 22 '22 at 00:31
  • lol code 0 should be good enough just was worried that the name warning is still there Thanks – user17997828 Jan 22 '22 at 00:33
  • See https://stackoverflow.com/questions/69146879/warning-c6387-visual-studio The compiler isn't smart enough to detect that the check prevents that problem. – Barmar Jan 22 '22 at 00:34
  • But https://stackoverflow.com/questions/66821678/c6387-for-memcpy-strcpy-and-strcpy-s says that adding the null check should work. Maybe it depends on the version of MSVC. – Barmar Jan 22 '22 at 00:36
  • Thanks again and I will check that link – user17997828 Jan 22 '22 at 00:37