#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Question Passing an array of pointers to a function


    Hi I've the following code:

    // 3 strings
    char str1[] = "str1";
    char str2[] = "str2";
    char str3[] = "str3";

    void func1()
    {
    char *strings[3]; // Array of 3 pointers to char

    // Assigning each string's(Character array's) address
    // to each pointer in the array.
    strings[0] = str1;
    strings[1] = str2;
    strings[2] = str3;

    // pass it to the second function
    func2(strings);
    }

    // Second function accepts pointer to pointer
    void func2(char *strings[])
    {
    printf("%s", strings[0]);
    printf("%s", strings[1]);
    }
    Are there any problems with this kind of usage? Thanks.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,403
    Rep Power
    1871
    Looks OK.

    Can you use [code][/code] tags in future, for code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo