0

My question is how can I split argv into two parts and store the parts in two variables.

I want my program to get parameters then a delimiter and then again parameters like this:

./program parameter1 parameter2 \: parameter3 parameter4

As you can see, the escaped ':' will act like a delimiter, cutting the array in two parts. Now I want to get a char *arr1[] which will hold parameter1 and parameter2 and another char *arr2[] which will hold parameter3 and parameter4. The parameters can be of different length.

How can I split argv and save the various parameters in two char *[]? In the end I want to access (with my example in mind) arr1[0] and it should have the string parameter1 inside it, and arr1[1] should have the string parameter2 inside it.

Edit: The problem lies in saving the parameters into the two different char *arr[]. I don't know how to do that, because I only know how to initialize an array with a value.

char *arr1[]; !error
char *arr1[10]; works, but what if I have more than 10 parameters?

Thanks in advance!

3
  • Do you know how to declare arr1 and arr2? Do you know how to iterate over argv? Do you know how to compare strings? etc. Please show as much of the code as you know how to do as a minimal reproducible example. Otherwise it is difficult to know exactly which parts you can do and which part you need help with. Commented May 17, 2022 at 20:21
  • @kaylum I agree, I was not clear in asking my question. The problem lies in saving the parameters into the two different char *arr[]. I don't know how to do that, because I only know how to initialize an array with a value. Will edit! Commented May 17, 2022 at 20:25
  • @Oka Thank you for your comment, that is a great and better way to get the start of the second part of parameters! Commented May 17, 2022 at 20:25

1 Answer 1

1

Don't! These are all pointers. Instead, use pointers - start pointer and end pointer or start pointer and count of element - to represent a "range" inside the source array.

char **arr1 = &argv[1];
char **arr1end = arr1;
while (*arr1end != NULL) {
    if (strcmp(*arr1end, ":") == 0) {
         break;
    }
    arr1end++;
}
if (arr1end == NULL) { /* handle error - user did not give : argument */ }
size_t arr1cnt = arr1end - arr1;
// Array arr1 has arr1cnt elements.

char **arr2 = arr1end + 1;
char **arr2end = arr2;
while (*arr2end != NULL) {
    ++arr2end;
}
size_t arr2cnt = arr2end - arr2;
// arr2cnt represents arguments after `:`.

for (size_t i = 0; i < arr1cnt; ++i) {
      printf("arr1[%zu]=%s\n", i, arr1[i]);
}
for (size_t i = 0; i < arr2cnt; ++i) {
      printf("arr2[%zu]=%s\n", i, arr2[i]);
}
Sign up to request clarification or add additional context in comments.

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.