Convert int array to char array

Discussion in 'C Programming' started by Frank Liebelt, Sep 27, 2007.

  1. Hi

    I try to convert a int array into a char array. My code: void exec()
    {
    char mds[32];
    int i;

    int mdc[32] =
    {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};

    for(i=0; i < 32; i++) {
    sprintf(mds, "%s", (char*)mdc);
    }

    printf("Result : %s\n", md5s);

    }

    If i compile this i got one compiler warning:
    warning: passing arg 1 of `sprintf' makes pointer from integer without
    a cast

    Cause this was the only warning i tried to run the program but get allways
    a Segmentation Fault.

    I dont know what to do. Uncle Goolge doesn't help me. Sorry, maybe i use
    the wrong search terms.

    Who could tell me whats going wrong?

    Frank
    Frank Liebelt, Sep 27, 2007
    #1
    1. Advertising

  2. Frank Liebelt

    Mark Bluemel Guest

    Frank Liebelt wrote:
    > Hi
    >
    > I try to convert a int array into a char array. My code: void exec()
    > {
    > char mds[32];
    > int i;
    >
    > int mdc[32] =
    > {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};
    >
    > for(i=0; i < 32; i++) {
    > sprintf(mds, "%s", (char*)mdc);
    > }
    >
    > printf("Result : %s\n", md5s);
    >
    > }
    >
    > If i compile this i got one compiler warning:
    > warning: passing arg 1 of `sprintf' makes pointer from integer without
    > a cast


    The first argument to sprintf() should be the address of a character
    buffer... You pass it an uninitialised character ...

    >
    > Cause this was the only warning i tried to run the program but get allways
    > a Segmentation Fault.


    You've told sprintf() that you will be passing it a pointer to a
    null-terminated string. You instead take an arbitrary integer and cast
    it to a pointer - what do you expect?

    > Who could tell me whats going wrong?


    It may be that I'm wasting my time talking to a troll...
    Mark Bluemel, Sep 27, 2007
    #2
    1. Advertising

  3. On 27 Sep, 10:41, Frank Liebelt <> wrote:

    > I try to convert a int array into a char array. My code: void exec()


    I think you're trying to convert the int into its corresponding
    string ie. into a char*. So you want an array of char*.


    > {
    > char mds[32];


    you want an array of char* or an array of array of char

    char mds [32][8];


    > int i;
    >
    > int mdc[32] =
    > {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,­99,98,102,55,101,50,53,100,49,53};
    >
    > for(i=0; i < 32; i++) {
    > sprintf(mds, "%s", (char*)mdc);


    - mds is of type char whilst sptintf() wants char*
    - the format for int is %d not %s
    - the cast does nothing useful. An int is not a char*
    don't put casts in just to get rid of compiler errors.

    sprintf(&mds, "%d", mdc);



    > }
    >
    > printf("Result : %s\n", md5s);


    what is md5s?


    >
    > }
    >
    > If i compile this i got one compiler warning:
    > warning: passing arg 1 of `sprintf' makes pointer from integer without
    > a cast


    because you did...


    > Cause this was the only warning i tried to run the program but get allways
    > a Segmentation Fault.
    >
    > I dont know what to do. Uncle Goolge doesn't help me. Sorry, maybe i use
    > the wrong search terms.
    >
    > Who could tell me whats going wrong?


    --
    Nick Keighley
    Nick Keighley, Sep 27, 2007
    #3
  4. "Frank Liebelt" <> schrieb im Newsbeitrag
    news:...
    > Hi
    >
    > I try to convert a int array into a char array. My code: void exec()
    > {
    > char mds[32];
    > int i;
    >
    > int mdc[32] =
    > {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};
    >
    > for(i=0; i < 32; i++) {
    > sprintf(mds, "%s", (char*)mdc);

    you're not deraling with strings (NUL terminated char arrays), so don't use
    sprintf here.

    mds=(char)mds; /* cast to silence a compiler warning about loss of
    precision and possibly sign (if char is unsigned in your implementation),
    which seems exaclty your intention */

    > }
    >
    > printf("Result : %s\n", md5s);
    >
    > }
    >
    > If i compile this i got one compiler warning:
    > warning: passing arg 1 of `sprintf' makes pointer from integer without
    > a cast

    Because you do. mds is not a char * but a char

    > Cause this was the only warning i tried to run the program but get allways
    > a Segmentation Fault.

    because mds is not a NUL termainated char array. The cast doesn't turn it
    into such a thing but instead lies to the compiler and the compiler then
    takes it's revenge...

    Bye, Jojo
    Joachim Schmitz, Sep 27, 2007
    #4
  5. @Mark
    No, you dont talk to a troll i am just a beginner.

    @Nick
    printf("Result : %s\n", md5s);

    I dont copy/paste the code. I typed it by hand and this was a mistake.
    This should be: printf("Result : %s\n", mds);

    @Joachim
    I tried your solution and it worked. Thanks.
    But now i have only one problem left.

    The int array is as char: 2da3e2c307df75e8d0e5e4cbf7e25d15
    but printf("Result : %s\n", mds); shows me
    2da3e2c307df75e8d0e5e4cbf7e25d15
    How could i remove this character after the last 5 and where it comes from?
    The array has a size of 32 but this above are 33.

    Sorry if this are dumb questions but i still start to learn C.

    Frank
    Frank Liebelt, Sep 27, 2007
    #5
  6. "Frank Liebelt" <> schrieb im Newsbeitrag
    news:...
    > @Mark
    > No, you dont talk to a troll i am just a beginner.
    >
    > @Nick
    > printf("Result : %s\n", md5s);
    >
    > I dont copy/paste the code. I typed it by hand and this was a mistake.
    > This should be: printf("Result : %s\n", mds);
    >
    > @Joachim
    > I tried your solution and it worked. Thanks.
    > But now i have only one problem left.
    >
    > The int array is as char: 2da3e2c307df75e8d0e5e4cbf7e25d15
    > but printf("Result : %s\n", mds); shows me
    > 2da3e2c307df75e8d0e5e4cbf7e25d15
    > How could i remove this character after the last 5 and where it comes
    > from?
    > The array has a size of 32 but this above are 33.
    >
    > Sorry if this are dumb questions but i still start to learn C.

    Sorry, I've missed that you tried tp prinft that char array.
    The problem is, that it still isn't a string, as it doesn't have a
    terminating NUL (at least not at the right place)

    Make that char array one element bigger (mds[33]) and place a NUL in the
    last element (mds[32]='\0';)
    Joachim Schmitz, Sep 27, 2007
    #6
  7. Frank Liebelt

    Army1987 Guest

    On Thu, 27 Sep 2007 11:41:17 +0200, Frank Liebelt wrote:

    > Hi
    >
    > I try to convert a int array into a char array. My code: void exec()
    > {
    > char mds[32];
    > int i;
    >
    > int mdc[32] =
    > {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};
    >
    > for(i=0; i < 32; i++) {
    > sprintf(mds, "%s", (char*)mdc);

    This tries to copy whatever is in memory location 50 (or 100, or
    97...) into wherever uninitialized mds[0] (or mds[1]...) happens
    to point when converted to a char *.
    You meant for(i=0; i<32; i++) { mds = mdc; }, right?
    > }
    >
    > printf("Result : %s\n", md5s);

    What is md5s? I guess a file scope variable, but you didn't touch
    it. (Or are the magic numbers in that array its address? If so,
    whatever you're trying to do is highly platform-specific).
    > }
    >
    > If i compile this i got one compiler warning:
    > warning: passing arg 1 of `sprintf' makes pointer from integer without
    > a cast
    >
    > Cause this was the only warning i tried to run the program but get allways
    > a Segmentation Fault.
    >
    > I dont know what to do. Uncle Goolge doesn't help me. Sorry, maybe i use
    > the wrong search terms.
    >
    > Who could tell me whats going wrong?
    >
    > Frank


    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
    Army1987, Sep 27, 2007
    #7
  8. Am Thu, 27 Sep 2007 12:30:40 +0200 schrieb Frank Liebelt:

    > How could i remove this character after the last 5 and where it comes from?
    > The array has a size of 32 but this above are 33.
    >
    > Sorry if this are dumb questions but i still start to learn C.


    Resolved.
    char mds[33]; instead of char mds[32]; to have space for \0 at the end.

    Found in my C Book.

    Frank
    Frank Liebelt, Sep 27, 2007
    #8
  9. Frank Liebelt wrote:
    > Hi
    >
    > I try to convert a int array into a char array. My code: void exec()
    > {
    > char mds[32];
    > int i;
    >
    > int mdc[32] =
    > {50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};
    >
    > for(i=0; i < 32; i++) {
    > sprintf(mds, "%s", (char*)mdc);


    mds is a char (an integer), not an array, or address
    of an array, or a pointer to an array. The first
    argument to sprintf() must be compatible with a
    pointer-to-char, which an integer is not.

    mdc is an int, and it makes no sense to cast it to a
    pointer
    > }


    Even if your code did what you seem to want it to do (see
    below), you have not null-terminated the array. In fact, you
    _can't_, because you haven't provided space for that '\0' byte.
    That means that mds is not a string and the following (with the
    typo corrected) is an error.

    > printf("Result : %s\n", md5s);

    ^^^^
    Cut-and-paste your actual code. This typo would not then creep
    in.
    >
    > }


    #include <stdio.h>

    void exec()
    {

    int mdc[] =
    { 50, 100, 97, 51, 101, 50, 99, 51, 48, 55, 100, 102, 55, 53,
    101, 56, 100, 48, 101, 53, 101, 52, 99, 98, 102, 55, 101, 50,
    53, 100,
    49, 53
    };
    size_t nchars = sizeof mdc / sizeof *mdc, i;
    char mds[1 + nchars];

    for (i = 0; i < nchars; i++) {
    mds = mdc;
    }
    mds[nchars] = 0;
    printf("Result (encoding is implementation-specific):\n \"%s\"\n",
    mds);

    }

    int main(void)
    {
    exec();
    return 0;
    }


    Result (encoding is implementation-specific):
    "2da3e2c307df75e8d0e5e4cbf7e25d15"

    >
    > If i compile this i got one compiler warning:
    > warning: passing arg 1 of `sprintf' makes pointer from integer without
    > a cast


    Your cast masked one of your other errors.
    >
    > Cause this was the only warning i tried to run the program but get allways
    > a Segmentation Fault.
    >
    > I dont know what to do. Uncle Goolge doesn't help me. Sorry, maybe i use
    > the wrong search terms.


    Your C text should tell you what the arguments to sprintf are, and
    should tell you the difference between a char and something that will
    decay to a pointer-to-char as an argument. Google is not the answer to
    everything.

    >
    > Who could tell me whats going wrong?


    Much. See above.

    >
    > Frank
    Martin Ambuhl, Sep 27, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,199
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    398
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,615
    Old Wolf
    Jan 20, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
  5. gert
    Replies:
    20
    Views:
    1,144
Loading...

Share This Page