简体   繁体   English

如何将字符串数组的元素复制到另一个字符串变量?

[英]how to copy an element of a string array to another string variable?

int main(int argc, string argv[])
{

this is what I want to do but it obviously won't work这是我想做的,但显然行不通

    string key = argv[1]; 

here I want to use the 'key' string as an array of characters to deal with a specific character alone这里我想使用'key'字符串作为一个字符数组来单独处理一个特定的字符

    key = key[1] + 1; 
    printf("%s", key);
}

the main function is not well formatted . main 函数格式不正确。 argv is a 2D array. argv 是一个二维数组。

int main(int argc, char **argv)

then you can malloc your key variable and use for example the strcpy function to copy argv[1] in the key variable.然后您可以 malloc 键变量并使用例如strcpy函数将 argv[1] 复制到键变量中。

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

int main(int ac, char **av){

    char *key = strdup(av[1]);

    printf("%s", key); 

    free(key)

    return (0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM