搜尋此網誌

[C/C++][pass array of character pointers]

主要是想傳遞 argv

/*
* Author: MH
* Since 2018/04/26
* Toolkit: Code::Block 16.01
*/

#include <stdio.h>
#include <stdlib.h>

int printArrayOfCharPointers (int argc, char *argv[])
{
    int i;
    for (i=0; i<argc; i++)
    {
        printf("argv[%d] %s\n", i, argv[i]);
    }
    /*
    print the following messages
    argv[0] abc123
    argv[1] mh-resource
    argv[2] ##**##
    */
    return 0;
}

int main(int argc, char *argv[])
{
    int argc_tmp = 3;
    char *argv_tmp[3];
    char p1[256];
    char p2[256];
    char p3[256];

    memcpy(p1, "abc123", sizeof("abc123"));
    argv_tmp[0] = p1;
    memcpy(p2, "mh-resource", sizeof("mh-resource"));
    argv_tmp[1] = p2;
    memcpy(p3, "##**##", sizeof("##**##"));
    argv_tmp[2] = p3;

    printArrayOfCharPointers(argc_tmp, argv_tmp);

    return 0;
}

沒有留言:

張貼留言