0

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?

10
  • 3
    The types const char arg_arr[][100] and const char *argv[] are incompatible. You can't convert the former to the latter; you need to rewrite the former. Commented Jul 13, 2020 at 14:06
  • 1
    const char arg_arr[][100] and const char *argv[] differ. char [][100] is an array of arrays of 100 chars. const char *argv[] is an array of pointers to const char. Commented Jul 13, 2020 at 14:06
  • try void ParseCmdLine(const char (*argv)[100]) {} Commented Jul 13, 2020 at 14:09
  • @JosephSible-ReinstateMonica but how to do it? Or there is no way to pass these params? Commented Jul 13, 2020 at 14:10
  • @KamilCuk but how to do it? Or there is no way to pass these params? Commented Jul 13, 2020 at 14:11

4 Answers 4

3

Change:

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

To:

 const char *arg_arr[] = {
 ...
};
Sign up to request clarification or add additional context in comments.

2 Comments

Not forcing anything, just what was in the question. Copy pasta
@AlekseyTimoshchenko: 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, or both as is done for the arguments of the main() function.
2

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.

Comments

0

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.

Comments

-3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.