I'm trying to make a table of binary strings of different sizes, without the values comporting only 0's. I'm very probably not using the best technique to do that but still, I think there is some way for this to work.
My question is about the function
void addbinarystring(char **prev, int size)
Here is my code:
#include <iostream>
using namespace std;
void addbinarystring(char **prev, int size) {
cout << (prev+size);
if (*prev[0] == '0') {
cout << "one";
*(prev+size)[0] = '1';
} else if (*prev[0] == '1') {
*(prev+size) = '0';
addbinarystring(prev-1, size);
}
}
int main() {
char *size1[1];
size1[0] = (char*) malloc(sizeof(char));
size1[0] = "1";
char *size2[3];
size2[0] = (char*) malloc(6*sizeof(char));
size2[0] = "01";
size2[1] = "10";
size2[2] = "11";
char *size3[7][3];
size3[0][0] = (char*) malloc(21*sizeof(char));
size3[0][0] = "0";
size3[0][1] = "0";
size3[0][2] = "1";
for (int i = 0; i < 7; i++)
{
addbinarystring(&size3[i][2], 3);
}
}
I don't understand why I get a segmentation fault error on the lines with *(prev + size)
, I thought it would access the memory where my next binary string is located. I have initialized the memory with malloc.
as I said, this is probably not the best way to get where I want to, but now that I'm in it I really want it to work. Thanks for any help!