char array

hi dear all

I am a new member , my question is about store one char array with 2 for loop to one char 2D array . I write the code and when call this function, it did not give any error but I could not see the result . this is my code :

class RArrays
{
public:
static char** RCharArray(int Size1, int Size2)
{
char** Array = new char*[Size1];
for (char Array1 = 0; Array1 < Size1; Array1++)
{
Array[Array1] = new char[Size2];
}
return Array;
}
};

char combine()
{
int j,i;
char buffer1[33];
char buffer2[33];
char **mix;
mix=RArrays::RCharArray(N,m);


for (j = 0 ; j < N ; j++)
{
_itoa_s (j,buffer1,2);
_itoa_s (makerand(new int[N]),buffer2,2);
strcat_s(buffer1,buffer2);

for(i=0 ; i<m ; i++)
{
while (buffer1[i]!=0)
{
mix[j][i] = buffer1[i];
printf("%c" , mix[j][i]);
}
}

printf ("\n") ;
}
return **mix ;
}

would you please answer my question ?

Regards
Last edited on
The class RArrays is redundant, there's nothing in it.

The problem with RCharArray is that it moves the pointer of the char* array. You need to remember that address as that is the base address of your 2D array. So the code should look somthing like:
1
2
3
4
5
6
7
8
9
char** RCharArray(int Size1, int Size2)
{
    char** Array = new char*[Size1];
    for (int i = 0; i < Size1; ++i)
    {
        Array[i] = new char[Size2];
    }
    return Array;
}


Please use the code format tag to format your code.

Thanks for your answer

But with this class , program did not get my char 2D array . Although I solved my problem by changing some part , now in the part of program that I change number to binary and store it in buffer when I display it some of numbers like 0 that should be 000 or 1 taht should be 001 had been shown just 0 or 1 and I need all three digit ?!

do you think what I shoud do ?

Regards
I'm sorry, I don't understand the question.

ok , I explain it again

I wrote one program . for example in this program , I give number 8 to program and then it changes numbers 0 - 7 to binary and show the new numbers in 3 digit , like 4 is 100 . But there is a problem here that 0 , 1, 2 , 3 were shown 0 , 1 , 10 , 11 instead of 000 , 001 , 010 , 011 . I should said that I store every result in one char array[33] .

Now I want to know what can I do that get correct result ?

I hope , you can answer my question :(

Regards
So to restate the problem, you're doing decimal to binary number conversion. The converted number is held in a char array. You want to see leading zeros and the code that prints the number is:
1
2
3
4
5
6
7
8
9
10
		for(i=0 ; i<m ; i++)
		{
			while (buffer1[i]!=0)
			{
				mix[j][i] = buffer1[i];
				printf("%c" , mix[j][i]);
			}
		}

		printf ("\n") ;


The code prints the digits one character at a time from a char array where the appear at the start of array.

If you can guarantee that the array is always null terminated, you can use printf's formatting to produce the output you like.

For example:
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int main()
{
        char s[] = "1";
        printf("%04s\n", s);    // width:4 with leading zeros
        printf("%0*s\n", 5, s); // width:5 with leading zeros

        return 0;
}

The second example shows how the width can be a parameter.

Dear kbw

Thank you so much
it was a good point and help to me

Regards
Topic archived. No new replies allowed.