通过命令行参数读取 任意 字符串, 可将其倒置不改变内容 eg 将 i am from china 倒置为 china from am i

int main(int argc, char *argv[])
{
    char *a[10] = {0};
    int i, j = 1;

    for(i = argc - 1; i > 0; i--)
    {
        a[j] = (char *)malloc(sizeof(char));   //指针数组中的每个值都是指针,使用时需要为它分配地址
        strcpy(a[j], argv[i]);
        j++;
    }

    for(i = 1; i < argc; i++)
    {
        printf("%s  ", a[i]);
    }
    return 0;
}

强调:   指针数组中的每个值都是指针,使用时需要为它分配

关于指针需不需要使用  malloc 分配内存

指针定义时需要初始化  让其 指向 NULL  然后在使用到时需要使用 malloc 

猜你喜欢

转载自blog.csdn.net/weixin_42720316/article/details/81739444
今日推荐