Output of an array using pointers

I have some problems understanding how to solve my homework.
After about 6h of experimenting I thought to get some help.

The Task:
I have to give out an included one-dimensional char array via the console.
However I have to use (2) pointers.
The linewidth of each line has to be 50 characters.

The problem:
While the task itself is not very complicated I don't know how to use the 2 pointers given in a useful way.
The pointers are declared as char *line and char *letter.
I use *letter[i] to get the symbols from the array.
To do this for the entire array I use a while loop.
I then limit the width of each line by including at the end of the while loop:

1
2
if((i+1)%50==0)   //i+1 because the array starts with [0] and it needs to be true at [49]
  cout << endl;


Now where do I get to use the *line pointer?
Any hints are greatly appreciated.
Last edited on
I would assume you use the "line pointer" to keep track of the current line of your array, as I assume that the array is declared as an array of C-Strings. Apart from that, I can't think of much use.
Not really. The array is in fact just a compilation of 1350 ASCII characters (poor bastard who wrote the task). So I don't need to keep track of the current line.
Anyways thanks though. I will use the pointer to add a number with the current line in front only because the pointer makes at least some sense then.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstring>
#include <iostream>

void print(const char* str, unsigned width)
{
    const char* line = str;
    const char* end = line + std::strlen(line);
    const char* letter = line;

    while ( line < end )
    {
        while ((letter - line) < width && letter != end)
            std::cout << *letter++;

        std::cout << '\n';
        line += width;
    }
}

int main()
{
    const char* s = "Supercalifragilisticexpialidocious";

    print(s, 2);
}


http://ideone.com/l7XyD2
Thanks a lot
Topic archived. No new replies allowed.