Solved

problem with character arrays... want to use pointers

Posted on 2004-10-26
188 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
Question by:somanchivasu
    2 Comments
     
    LVL 5

    Accepted Solution

    by:
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now