Sirop4ik
Sirop4ik

Reputation: 5243

How to get pointer on array?

I have such an array:

    const char arg_arr[][100] = {
        "a",
        "b",
        "c",
        "d",
    };

and then I have such a method ParseCmdLine(const char *argv[]);

So, in order to pass my arg_arr I need to get pointer to it.

I try to invoke this method such way ParseCmdLine(*arg_arr) or like this ParseCmdLine(&arg_arr), but it doesn't work.

Question is - how to pass this arr as a pointer?

Upvotes: 0

Views: 182

Answers (4)

bogdyname
bogdyname

Reputation: 374

The name of the array is essentially the address of its first element.

Upvotes: -3

user11381830
user11381830

Reputation:

How to get pointer on array?

Available options regarding what you have provided ( without changing your code ):

Numerically:

enter image description here

Alphabetically:

enter image description here

Here's a guide that I think might help.

Upvotes: 0

chqrlie
chqrlie

Reputation: 144740

arg_arr is a constant array of arrays of char, which is a different type from what ParseCmdLine expects: a pointer to an array of pointers to constant char arrays.

You should define arg_arr this way:

const char *arg_arr[] = {
    "a",
    "b",
    "c",
    "d",
    NULL
};

and pass it directly as ParseCmdLine(arg_arr).

Note that ParseCmdLine must have a way to tell how many elements are present in the array. Either pass this count as an extra argument (argc) or add a trailing NULL pointer after the last string as shown above, or both as is done for the arguments of the main() function.

Upvotes: 2

Waqar
Waqar

Reputation: 9331

Change:

 const char arg_arr[][100] = {
 ...
};

To:

 const char *arg_arr[] = {
 ...
};

Upvotes: 3

Related Questions