连接c中命令行参数的第一个字符

Concatenate first characters from a command line argument in c

我试过这个解决方案,但不知道为什么会出现分段错误。

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

int main(int argc, char *argv[])
{
    char st[1000]="";
    for (int i=1; i<argc; i++)
    {

            strcat(st,argv[i][0]);
            strcat(st,". ");

    }
    printf("%s", st);

    return 0;
}

更改此声明

        strcat(st,argv[i][0]);

        strncat(st, &argv[i][0], 1);

        strncat(st, argv[i], 1);

在原始语句中,函数将字符 argv[i][0] 视为指针值。所以这个调用有未定义的行为。

或者您可以使用不使用字符串函数的直接方法。例如

#include <stdio.h>

int main( int argc, char * argv[] ) 
{
    char st[1000];
    size_t i = 0;

    for ( int j = 1; j < argc; j++ )
    {
        st[i++] = *argv[j];
        if ( j + 1 != argc )
        {
            st[i++] = ',';
            st[i++] = ' ';
        }           
    }

    st[i] = '[=13=]';

    puts( st );

    return 0;
}

试试这个:

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

int main(int argc, char *argv[]){
    char st[1000]="";

    --argc, ++argv;//skip first.
    char *p = st;
    for (int i = 0; i < argc; i++){
        if(i){//between ?
            strcpy(p, ". ");
            p += 2;
        }
        *p++ = argv[i][0];//copy one character
    }
    printf("%s\n", st);

    return 0;
}

完全不使用任何字符串库函数的解决方案可能如下所示:

#include <stdio.h>

#define ARGV_MAX (5) /* Adjust this to match the maximum number of arguments
                        to be processed. */

#define RESULT_STR_MAX (3*ARGV_MAX +1) /* The chararacters per arg per maximum arguments 
                                          to be processed +1 for  the 0-terminator. */

int main(int argc, char *argv[])
{
    char st[RESULT_STR_MAX] = ""; 
    size_t string_index = 0;
    size_t argv_index = 1; /* Skip argv[0] below as it does not carry an
                              argument, but the program's name. */

    while ((size_t) argc > argv_index 
           && RESULT_STR_MAX > string_index /* in fact this or 
           && ARGV_MAX >= argv_index)          this would do. */
    {
      st[string_index] = argv[argv_index][0];
      string_index++;

      st[string_index] = '.';
      string_index++;

      st[string_index] = ' ';
      string_index++;

      argv_index++;
    }

    st[string_index] = '[=10=]'; /* Add the '0'-terminator 
                                to make the char array a "string". 
                                (not necessary as st got initialised to all 0s).*/

    if (ARGV_MAX < argv_index)
    {
      printf("Ignored the last %d argument(s).\n", argc - ARGV_MAX - 1);
    }

    puts(st);

    return 0;
}

下面使用 strcat() 的另一种方法:

#include <stdio.h>

#define ARGV_MAX (5) /* Adjust this to match the maximum number of arguments
                        to be processed. */

#define RESULT_STR_MAX (3*ARGV_MAX +1) /* The chararacters per arg per maximum arguments 
                                          to be processed +1 for  the 0-terminator. */

int main(int argc, char *argv[])
{
    char st[RESULT_STR_MAX] = ""; 
    size_t argv_index = 1; /* Skip argv[0] below as it does not carry an
                              argument, but the program's name. */

    while ((size_t) argc > argv_index 
           && ARGV_MAX >= argv_index)          
    {
      strcat(st, (char[2]){argv[argv_index][0]});
      strcat(st, ". ");

      argv_index++;
    }

    if (ARGV_MAX < argv_index)
    {
      printf("Ignored the last %d argument(s).\n", argc - ARGV_MAX - 1);
    }

    puts(st);

    return 0;
}