need help on a collection of strings

This is a discussion on need help on a collection of strings within the C Programming forums, part of the General Programming Boards category; The following code accepts as parameters 1) a pointer to an array of long values and 2) an int containing ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    need help on a collection of strings

    The following code accepts as parameters 1) a pointer to an array of long values and 2) an int containing the number of elements in that array. The code loops through each element in the array, produces a string representing the corresponding long's value in binary, and prints the binary representation to stdout. It then returns an int.

    The constant in the malloc (33) is there because the max length of the string is 32 (bits in a long) +'\0'.

    What I would REALLY like to do is:
    1) include (as an additional parameter) a pointer (char ***binrep??) declared in main 2) store (rather than print) each binary representation as a string in an array(referenced by the pointer passed from main) with the same index as it's long counterpart. For example, if element 0 of elearray is 7, then element 0 of binrep should be 111. The array (or rather, the pointers) are source of my problem.

    I could then use the pointer (and pointer math) that was declared in main to access the elements of the array.

    For the sake of furthering my understanding, I would prefer to use strictly pointers (NO []) because multiple levels of indirection is a real issue for me. I know there are many of you for whom this is a no-brainer, but I am really struggling trying to get the pointers to work.

    Thanks

    Code:
    int conv2bin(long *elearray , int elemcount)
    {
    int i;
    char *binrep=(char *)malloc(33*sizeof(char *));
    
        for (i=0;i<elemcount;++i)
        {
    
            _ltoa( *(elearray+i), binrep,2 );
            printf( "String of long int %ld (radix 2): %s\n", *(elearray+i), binrep);
        }
    
        return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The malloc isn't really necessary, also you've coded it incorrectly.

    How about this version, does it help you?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void convtobin(long *list, int count)
    {
        char buf[BUFSIZ];
        
        while (count--)
        {
            ltoa(*list, buf, 2);
            puts(buf);
            list++;
        }
    }
    
    int main(void)
    {
        long myarray[] = {1L, 876L};
        
        convtobin(myarray, sizeof(myarray) / sizeof(myarray[0]));
        
        return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Thank You, Hammer

    I'll post a result as soon as I can try it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21