1
int main(int argc, char **argv){

   char Q[MAXCHAR];
   Q=argv[k+1];}

Q is an array while argv[k+1] is a pointer. How can I get the content of argv[k+1] into Q?

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • pointer and array are the same thing, actually. You just need `strcpy`. – Sulthan Jul 02 '16 at 14:29
  • 5
    @Sulthan Pointers and arrays are not the same. Arrays decay to pointers in certain circumstances; that's all. – underscore_d Jul 02 '16 at 14:29
  • @underscore_d Isn't an array in C just a chunk of continuous memory identified by the pointer to the beginning? – Sulthan Jul 02 '16 at 14:31
  • 1
    @Sulthan An array is a distinct class of object, which is often _converted to a pointer_, but is not a pointer: http://stackoverflow.com/a/4608421/2757035 The name of the array retains important information, chiefly the array's `sizeof`, which a pointer is incapable of conveying. Hence the need to pass array sizes everywhere, of course. – underscore_d Jul 02 '16 at 14:34
  • @Sulthan contiguous chunk of memory yes, pointer no. – melpomene Jul 02 '16 at 14:35
  • 1
    Possible duplicate of [Proper way to copy C-strings](http://stackoverflow.com/questions/9593798/proper-way-to-copy-c-strings) – underscore_d Jul 02 '16 at 14:42
  • 1
    @Sulthan: Please (a) read section 6 of the [comp.lang.c FAQ](http://www.c-faq.com), and (b) delete your initial comment. The false idea that arrays are "really" pointers is one of the most common misconceptions about C. Please don't spread it further. – Keith Thompson Jul 02 '16 at 22:42

2 Answers2

0

You can use snprintf

snprintf(Q, MAXCHAR, "%s", argv[k]);

(edited : first version recommended strncpy).

Louen
  • 3,617
  • 1
  • 29
  • 49
  • `strncpy` is never what you want. If you want a truncating copy (itself a dubious method), use `snprintf`. – melpomene Jul 02 '16 at 14:32
  • @melpomene : Why would `strncpy`not be recommended in this case ? – Louen Jul 02 '16 at 14:34
  • Because `strncpy` should never be used. It does weird things and it's misnamed (it doesn't deal with C strings, for example, unlike the other `str*` functions). – melpomene Jul 02 '16 at 14:35
  • I used strncpy and got a wall of errors, i included the header tho. – asdasdasd1234 Jul 02 '16 at 14:35
  • `count == MAXCHAR` - _wow!_ That seems like a bad idea: "If, after copying the terminating null character from `src`, `count` is not reached, additional null characters are written to `dest` until the total of `count` characters have been written." - http://en.cppreference.com/w/c/string/byte/strncpy – underscore_d Jul 02 '16 at 14:39
  • OK. I found this which was more helpful as to why. I will edit my reply then. http://www.victordodon.com/c-functions-you-should-never-use-strncpy/ – Louen Jul 02 '16 at 14:39
  • 1
    The victordodon link doesn't mention that `strncpy` is also unnecessarily wasteful if `n` is big but `src` is short (which is a common case), because `strncpy` always writes exactly `n` chars to `dest` (if `src` is shorter, `'\0'` is written). – melpomene Jul 02 '16 at 14:45
  • [My own rant about `strncpy`](https://the-flat-trantor-society.blogspot.com/2012/03/no-strncpy-is-not-safer-strcpy.html) – Keith Thompson Jul 02 '16 at 22:43
0

You can't directly assign Q = argv[k+1]. For an array (Q[MAXCHAR]), arrayname (Q) is the base address. The base address of an array can't be changed. Assuming k = 0, you can use any of the following to get the argv[1] data into Q.

memmove(Q, argv[1], strlen(argv[1]) + 1);

or

snprintf(Q, strlen(argv[1]) + 1, "%s", argv[1]);

or

strncpy(Q, argv[1], strlen(argv[1]) + 1);

or

memcpy(Q, argv[1], strlen(argv[1]) + 1);

or

sprintf(Q, "%s", argv[1]);

or

strcpy(Q, argv[1]);

Here is the program and it's output using memmove:

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

#define MAXCHAR 20

int main(int argc, char **argv)
{
    if (argc < 2) {
        puts("Not enough arguments");
        return -1;
    }
    char Q[MAXCHAR] = {0};

    memmove(Q, argv[1], strlen(argv[1]) + 1);
    puts(Q);
    return 0;
}

Output:

me@linux:~$ ./a.out stackexchange
stackexchange
NayabSD
  • 1,112
  • 2
  • 15
  • 26