?
Solved

problem with character arrays... want to use pointers

Posted on 2004-10-26
4
Medium Priority
?
241 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:somanchivasu
2 Comments
 
LVL 5

Accepted Solution

by:
van_dy earned 1000 total points
ID: 12418859
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
 
LVL 12

Expert Comment

by:stefan73
ID: 12419474
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question