0

I'm trying to iterate an array which has 5 elements, each one with 1024 bytes. How can I iterate each byte of each element?

My code: Class.hh:

static char *sheets[5];

Class.cc:

#define SHEET_SIZE 1024
Class::sheets[0] = new char[SHEET_SIZE];

Because if I do:

for(int i = 0;i<5;i++)
{
    cout << sheets[i] << endl;
}

Wouldn't this print like the whole element and iterate one sheet at a time?

3
  • So you want to iterate over the elements of each sheet? Commented Dec 23, 2021 at 18:15
  • @ScottHunter yes I want to iterate over each one of them so I can check what value they have in them. Commented Dec 23, 2021 at 18:16
  • 2
    You used a loop to iterate over the sheets; why can't you use another to iterate over what is in a sheet? Commented Dec 23, 2021 at 19:09

3 Answers 3

1

One issue is that you don't know the length of each character array. Better to use std::string or std::vector<std::string>.

Here's some code to iterate over the matrix:

for (int row = 0; row < 5; ++row)
{
    // If row is a C-string then we could use a nul terminator.
    // Otherwise we'll assume a maximum length.
    static const int MAXIMUM_COLUMNS = 1024;
    for (int column = 0;
         (sheets[row][column] != 0) || (column < MAXIMUM_COLUMNS); 
         ++column)
    {
        Do_Something(sheets[row][column]);
        // Or
        std::cout << sheets[row][column];
    }
    std::cout << "\n";
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'd second using std::vector<std::string>.
OP had a very specific question about iteration on an existing data structure. You are not answering the question but suggesting an overhaul of his design and a different data structure. That's beyond the point.
@Jellyboy: If you study the OP's code, you'll note that there is a char * sheets[5];, which is a 2D character array (or it can be accessed like one). My answer shows how you can iterate through char * [5] as a 2d matrix. I have not overhauled the OP's design, nor have I changed the data structure. I'm going through the intent, which changed since your answer.
The length of the character array is said up there SHEET_SIZE. It is fixed.
@Jellyboy: The array of pointers is fixed at SHEET_SIZE. What are the sizes of the arrays that the pointers point to? For example, what is the length of *sheets[3]? Is it the size of a pointer? Or is it the length of C-Style string?
0

The value of sheets[i] is a char pointer which will hopefully be terminated with a zero char otherwise it will segfault your application. If the zero char is at the beginning or in the middle it will not print the entire array so yes, it will fail your requirement.

If you want to iterate over every char of every array you have to do a little bit more:

for(int i = 0;i<5;i++)
{
    cout << "Sheet " << i << endl;
    for ( int j=0; j<SHEET_SIZE; ++j ) {
        cout << sheets[i][j];
    }
    cout << endl;
}

5 Comments

I get a segmentation fault. Not sure if its because the all of the sheets are empty but I don't know how to put anything in them either
@Average_C_Enjoyer I get a segmentation fault. Create a minimal reproducible example
@eerorika well I just copied the code on top of mine and got it. What else do I need to post?
@Average_C_Enjoyer In that case the reason is that the pointers in sheets, except for sheets[0] are null. You may not indirect through null pointers. In order to iterate elements pointed by a pointer, you must first initialise the pointer to point to elements.
@eerorika my hunch is that you are allocating only the first element sheets[0]. You need to allocate them all: sheets[1], sheets[2], sheets[3] and sheets[4].
0

Wouldn't this print like the whole element and iterate one sheet at a time?

It depends on what you have in a sheet. If they are only printable characters (ASCII code between 32 and 126) ending with a null character (0x00), you would get an expected output (1023 characters). But, if your sheet contains non-printable characters you won't get a "good" output; and the print will be cut once a null character (0x00) appears within a sheet.

You can have more control over each char in a sheet if you walk each sheet just as any other array. For example you could treat each sheet as an array of bytes, not just as a string, and dump its contents in hex:

  • Walk the sheets array, and, for each sheet,
  • walk it as well, get each individual char, extract top and bottom nibbles, print them.

[Demo]

#include <iostream>  // cout
#include <numeric>  // iota

const size_t number_of_sheets{5};
const size_t sheet_size{5};

void print(char* sheet)
{
    for (int i{0}; i < sheet_size; ++i)
    {
        auto to_string = [](auto c) -> char {
            if (0 <= c and c <= 9) { return c + '0'; }
            if (10 <= c and c <= 15) { return c - 10 + 'a'; }
        };
        auto bottom_nibble{(sheet[i] & 0x0f)};
        auto top_nibble{((sheet[i] >> 4) & 0x0f)};
        std::cout << (i ? "\'" : "") << to_string(top_nibble) << to_string(bottom_nibble);
    }
}

int main() {
    static char* sheets[number_of_sheets];
    for (int i{0}; i < number_of_sheets; ++i)
    {
        sheets[i] = new char[sheet_size];
        std::iota(sheets[i], sheets[i] + sheet_size, i * 0x11);
        std::cout << "Sheet " << i << ": ";
        print(sheets[i]);
        std::cout << "\n";
    }
}

// Outputs:
//
//     Sheet 0: 00'01'02'03'04
//     Sheet 1: 11'12'13'14'15
//     Sheet 2: 22'23'24'25'26
//     Sheet 3: 33'34'35'36'37
//     Sheet 4: 44'45'46'47'48

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.