Nested range-for loops with multi-dimensional array

This is a test program that takes a number of arguments from the command prompt and concatenates them into a string object. I was looking into the possibility of using the range-based for loop for this purpose. Can it be done with pointer based arrays? I am mainly doing this because I want to have a firm understanding of range-based for, but also would like to do this with least amount of code possible.

This is my working program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>

int main(int argc, char *argv[])
{
    if (argc > 1) {
        std::string concatenatedArgs;
        for (int argnr = 1; argnr != argc; ++ argnr) {
            while (*argv[argnr] != '\0') {
                concatenatedArgs += *argv[argnr]; // Add one character at a time.
                ++ argv[argnr]; // Step address forward to the next character.
            }
        }
        std::cout << concatenatedNames << std::endl;
    }
    return 0;
}


Can I somehow replace my while-loop with a range-based for? I tried the following but the compiler points out that begin and end were not declared in the scope of the range-based for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>

int main(int argc, char *argv[])
{
    if (argc > 1) {
        std::string concatenatedArgs;
        for (int argnr = 1; argnr != argc; ++ argnr) {
            for (char &c : argv[argnr]) // ERROR
                concatenatedArgs += c;
        }
        std::cout << concatenatedNames << std::endl;
    }

    return 0;
}
> using the range-based for loop for this purpose. Can it be done with pointer based arrays?

We do not have an array to work with. argv is not an array, it is a pointer to char*


> would like to do this with least amount of code possible.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main( int argc, char* argv[] )
{
    std::string concatenatedArgs;
    for( int i = 1 ; i < argc ; ++i ) concatenatedArgs += argv[i] ;
    std::cout << concatenatedArgs << '\n' ;
}
Well jeez, that was simple. :-) Thanks man.

I was under the impression that an array, or more specifically a C-style string in this case, is basically the same thing as a pointer to char?
Arrays are not CopyAssignable; they can't be passed by value.
Instead, the implicit conversion from array to pointer is applied and the resultant pointer is passed by value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

void foo( const char (&an_array) [100] ) // an_array is (a reference to) an array
{
    std::cout << "sizeof(foo::an_array): " << sizeof(an_array) << '\n' ; // 100
}

void bar( const char an_array[100] ) // an_array is not an array, it is a pointer
{
    std::cout << "sizeof(bar::an_array): " << sizeof(an_array) << '\n' ; // size of pointer
}

int main()
{
    const char this_is_an_array[100] = "C-style string" ;
    std::cout << "array: " << sizeof(this_is_an_array) << '\n' ; // 100

    const char* this_is_a_pointer = this_is_an_array ; // implicit conversion array => pointer
    std::cout << "pointer: " << sizeof(this_is_a_pointer) << '\n' ; // size of pointer

    foo(this_is_an_array) ;

    bar(this_is_an_array) ;
}

http://coliru.stacked-crooked.com/a/cd175da47a2ab6cc
Topic archived. No new replies allowed.