[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

problem with character arrays... want to use pointers

Posted on 2004-10-26
4
Medium Priority
?
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

649 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