0

For some reason, the following code compiles and runs fine, but after adding the C/C++ VsCode extension, I get a syntax error saying that there are too few arguments.

For background, I am receiving a user input in main, passing the string to the this function, where I am tokenizing the string using " \n" as a delimiter, and each token represents a different "command". My goal here is to store each token in a different index to validate them, then pass the originally given string, input to a different function, since it needs to be passed as one value.

int login(int socket, char *input) {
    if (socket > 0 && input != NULL && strlen(input) > 0) {
        char buffer[MAX_LINE];
        strcpy(buffer, input);
        strtok(buffer, " \n");

Simplified (easily reproducible) version of what I'm trying to do:

header:

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

int func(char*);

main:

#include "str_test.h"
#define MAX_LINE 256 

int main(){
   char*str_input = NULL;
   char buff[MAX_LINE];
   fgets(str_input, MAX_LINE, stdin);
   strcpy(buff, str_input);

   func(buff);

   return 0;
}

int func(char*str){
   char new_buff[MAX_LINE];
   strcpy(new_buff, str);
   return 0; 
}

The exact error message is too few arguments in invocation of macro "strcpy"C/C++(54) on both instances of strcpy().

My Makefile C Flags are: set(CMAKE_C_FLAGS "-std=c11 -Wall -Wextra -Wshadow -Werror")

Is this because of a invalid compiler path(s)? Considering I haven't had any actual compilation or runtime errors?

koncameron
  • 25
  • 5
  • 3
    Did you include the headers in this file? – tadman Feb 20 '23 at 18:42
  • 2
    Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives. This allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel Feb 20 '23 at 18:44
  • 1
    Please post the exact error message into the question as text. – Andreas Wenzel Feb 20 '23 at 18:46
  • 1
    Normally, `strcpy` takes just _two_ arguments. But, IIRC, MS has some defines for "safer" versions. You may be hitting those. If you look at the post-processed output (e.g. `cpp` or `cc -E -P`) you can look at the declaration given for `strcpy` to see what args it takes. Then, you can view the `.h` file with the declaration. – Craig Estey Feb 20 '23 at 18:50
  • 2
    @CraigEstey But those have different names, like `strcpy_s`, no? It makes no sense to make a nonstandard `strcpy` with three arguments; it just breaks code. – Kaz Feb 20 '23 at 18:54
  • 1
    @Kaz Yes, using `strcpy_s` _would_ be the sensible way. But, then, OP wouldn't have such an issue. And, IIRC, MS wanted their old code to not compile if using unsafe versions. So, somehow OP kicked that in. That's why I suggested getting the `cpp` output to see what is actually being used [no matter what a complex `#ifdef` chain produces]. – Craig Estey Feb 20 '23 at 18:59
  • I'm really more wondering about the C/C++ extension, since everything I am doing is syntactically and functionally legal. I can remake this in a simplified form so you don't need the context of the project if that would help. – koncameron Feb 20 '23 at 19:05
  • @koncameron Show the header "so_test.h". – Vlad from Moscow Feb 20 '23 at 19:53
  • 1
    After using `strtok( buffer...` in `main()`, `buffer` will be null terminated at the end of the first token... You won't get anything more from `buffer` being passed to the function. AND `int func(buffer);` is a misplaced function **declaration**, not **invocation.**.. – Fe2O3 Feb 20 '23 at 19:53
  • 1
    What you show is still not a [mre]. Please make sure that we can simply copy the sources into files and try to compile them, reproducing the error. BTW, please include the complete and exact error message, which includes file name and location. Optimally, tell us in the provided sources, where the error points to. – the busybee Feb 20 '23 at 20:56
  • @thebusybee sorry about that, updated it. – koncameron Mar 01 '23 at 21:18
  • VSCode is a text editor that can also run external commands. Your build does not care about VSCode extensions. You can open a terminal and run your build from there—and that's exactly what you should do to isolate the problem. If you have errors, copy **the entire set of messages, unedited**, filenames and line numbers and all, and paste to your question. `too few arguments in invocation of macro "strcpy"C/C++(54)` doesn't even look like a compiler message. – n. m. could be an AI Mar 01 '23 at 21:34
  • Since VSC seems to think that `strcpy` is a macro: I expect from a serious IDE that it can show macro expansions. Please hover over `strcpy` (or whatever triggers macro expansion in VSC) and look at the expanded text. It should be possible to get a (sub-)window to copy the expanded source. If you manage to do this, please [edit] your question and add that. – the busybee Mar 02 '23 at 06:55
  • Or, if VSC refuses to expand because of the "error", it should at least be able to jump to the macro's definition. Try this and add your findings, please. – the busybee Mar 02 '23 at 09:15

1 Answers1

1

It sounds like you should probably ignore the "C/C++ VSCode extension" warning:

  • strcpy() takes two arguments: you don't have "too many arguments".
  • strcpy() is typically a function, not a "macro".
  • As you said: your code compiles and runs fine :)

SUGGESTION:

It's possible the VSCode extension warning is being triggered by something else, adjacent to your "strcpy()".

Consider reproducing the problem in a minimal, reproducible example. If you can reproduce it in a small, standalone test,then be sure to copy/paste the exact error text, and the line#.

paulsm4
  • 114,292
  • 17
  • 138
  • 190