• C

problem with character arrays... want to use pointers

hi all,

I want to store some large number of characters into a character buffer array... i want to use pointers ..

I am converting some decimal numbers into binary values but now i want to store these binary values into an array which should be of infinite length... i have typecasted the binary values in char...  basically i can store these binary values into arrays of 16  bit length as i need to  sum every 16 bits of the binary values to each of the other 16 bits of binary values i generated.... for example... i have generated binary values of 16 4 13 56.... now i need to store the binary values of these numbers into an array and then split them 16 16 bits and add them...

I hope u got my point....would be very happy if u can help me out..

Thanq
somanchivasuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
i suppose you are trying to do something like this:

char binary[SOME_BIG_NUMBER];
int number = (some value);      //this is what you are going to convert
index i =0;
while(number){
        binary[i++] = (number % 2) + '0';   // suppose you get zero, you will store 48
        number /= 2;
}
binary[i] = 0;

// you get the binary representation of number in reverse order in binary[] from above. I guess
it will be fairly easy from here onwards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stefan73Commented:
Hi somanchivasu,
The easiest way is to use a fixed-size array, as suggested above. But that's far from elegant, because it can overflow, and if you make it big enough to fit all cases, you're wasting memory for most cases.

The best compromise is to use a dynamically allocated/reallocated string:

size_t cur_size=0;
size_t max_size= 128; /* Arbitrary initialization */
char* arr = (char*) malloc(max_size);

When you then insert something into your array, you can test for overflow and realloc accordingly:

while(cur_size + insert_size >= max_size){
    max_size *= 2;
    arr = (char*) realloc(arr,max_size);
}

Now you can insert and it will grow dynamically. Don't forget to update cur_size accordingly.



Cheers!

Stefan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.