Rella
Rella

Reputation: 66945

multidimentional char array - how to fill it with random chars?

So I have tried to create a generator like:

#include <iostream>
#include <iomanip>  
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
char* data;
void genRandomFilledChar(char *s, int i, int j, int k) { 
    const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
    for (int q = 0; q < i; ++q) {
        for (int w = 0; w < j; ++w) {
            for (int e = 0; e < k; ++e) {
                s[e] = alphanum[rand() % (sizeof(alphanum) - 1)];
            }
        } }}

int main()
{
    data =  new char[10000][10000][10000];
    genRandomFilledChar(data, 10000, 10000, 10000);
    cin.get();
    return 0;
}

But it fails to compile. What am I doing wrong?

Upvotes: 0

Views: 2801

Answers (5)

Andrey Sidorov
Andrey Sidorov

Reputation: 25456

C multidimensional arrays are just a way to calculate index automatically. Why not declare char data[i*j*k] (or get from heap with new) and then fill it as if it is single dimension? You can later use [][][] indexes with data.

Upvotes: 1

neuront
neuront

Reputation: 9612

Because new char[10000][10000][10000] has the type char (*)[10000][10000], not char*, the type of data. And I think what you want is

void genRandomFilledChar(char *s, int i, int j, int k) { 
    const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
    for (int q = 0; q < i; ++q) {
        for (int w = 0; w < j; ++w) {
            for (int e = 0; e < k; ++e) {
                s[e + w * k + q * j * k] = alphanum[rand() % (sizeof(alphanum) - 1)];
            }
        }
    }
}

int main()
{
    data =  new char[10000 * 10000 * 10000];
    genRandomFilledChar(data, 10000, 10000, 10000);
    cin.get();
    return 0;
}

Upvotes: 1

Xeo
Xeo

Reputation: 131789

char* data;
// ....
data =  new char[10000][10000][10000];

Are wrong. First, a three dimensional array of chars is char***. Next, you need to initialize that memory in 3 steps.

data = new char**[10000];
for(int i=0; i < 10000; ++i){
  data[i] = new char*[10000];
  for(int j=0; j < 10000; ++j){
    data[i][j] = new char[10000];
    for(int k=0; k < 10000; ++k){
      // and for best performance, initialize directly
      data[i][j][k] = /*HERE*/;
    }
  }
}

Upvotes: 1

Jordonias
Jordonias

Reputation: 5848

s[e] = alphanum[rand % (sizeof(alphanum) - 1)];

should be

s[q][w][e] = alphanum[rand % (sizeof(alphanum) - 1)];

because it is a 3 dimensional array

Upvotes: 1

DReJ
DReJ

Reputation: 1974

You can't allocate new array like you do. You should declare one dimensional array of proper size or create array of pointers to pointer to array.

char *data = new char [1000*1000*1000]; 

or

char ***data = new char**[1000];
for (int i=0; i< 1000; i++) {
    data[i] = new char*[1000];
    for (int j=0; j< 1000; j++) {
        data[i][j] = new char[1000];
    }
} 

Also you have error in genRandomFilledChar. You fill only first 10000 chars with random values every time.

And, the last. For rand() you need to initialize random generator with srand.

Upvotes: 2

Related Questions