-3
int main(int argc, char * argv[]){

   char file_extension[10];

   strncat(file_extension, argv[2][5], 6);

When I do this, I get "warning: passing arg 2 of 'strncat' makes pointer from integer without a cast'. Does does anyone know how to fix this?

user1472747
  • 529
  • 3
  • 10
  • 25
  • So C or C++? Remove the C++ tag if it's C. –  Feb 08 '13 at 19:05
  • 2
    Also, what are you trying to achieve? What you currently have has at least two UBs inside and it also makes absolutely no sense. –  Feb 08 '13 at 19:06
  • I'm trying to extract the file extension from an argument. I left out most of the other code to make this simple. What is a "UB"? – user1472747 Feb 08 '13 at 19:08
  • 2
    @userXXX For that, why don't you use `char *extension = 1 + strchr(argv[1], '.');`? –  Feb 08 '13 at 19:09
  • @user1472747 UB is an acronym for 'undefined behavior'. – vlad Feb 08 '13 at 19:13

4 Answers4

3

argv[2] is a char *, so that would make argv[2][5] a char

strncat accepts char * parameters only!

Forhad Ahmed
  • 1,761
  • 13
  • 18
2

argv[2][5] is a char only.

Try with just argv[2].

Adrián
  • 6,135
  • 1
  • 27
  • 49
2

So if I finally managed to comprehend correctly all your ambiguous comments: it seems that you want to copy a part of a string.

const char foo[] = "abcdefghijklm";
const size_t start = 3;
const size_t len = 5;

char the_copy[len + 1];
memcpy(the_copy, foo + start, len);
the_copy[len] = 0;
  • @user1472747 You're welcome. I'm not "eager". See, I was a beginner too, and it was not very long ago. Yet I didn't have to ask questions like this. Show some creativity! The documentation of the standard library functions is also helpful. And if you listen to one more advice: don't want to do everything the first day you start programming! It will take several months, years, etc. till you have the necessary experience for something complex. –  Feb 08 '13 at 19:26
1

You have several problems:

  • argv[2][5] is a single character: the 6th character of the 3rd input argument, but the second argument of strncat takes a pointer to a character (i.e. a C string), not a single character
  • file_extension is not null-terminated to begin with (it's uninitialized), so it's Undefined Behavior to call strncat on it
  • The last argument to strncat is the maximum number of characters of the source string to concatenate, not the size of the output buffer -- it does not protect you from buffer overflows.
  • If you actually meant to write strncpy instead of strncat, then you also need to be aware that strncpy does not necessarily null-terminate the output

There are are easy fixes to these problems, but the best fix for you depends on exactly what you're going to do: is the file extension read-only? Are you constructing a new filename? What's going to happen with it?

I strongly recommend you get a good book about C strings and C programming instead of trying to blindly write string code without having a solid understanding of what it's doing, especially with the high risk of buffer overflows and memory corruption when the code is not written correctly.

Community
  • 1
  • 1
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589