Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Convert int array to char array

Reply
Thread Tools

Convert int array to char array

 
 
Frank Liebelt
Guest
Posts: n/a
 
      09-27-2007
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,10 0,49,53};

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

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
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      09-27-2007
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,10 0,49,53};
>
> for(i=0; i < 32; i++) {
> sprintf(mds[i], "%s", (char*)mdc[i]);
> }
>
> 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...
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      09-27-2007
On 27 Sep, 10:41, Frank Liebelt <ecos...@hotmail.de> 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,1 00,49,53};
>
> for(i=0; i < 32; i++) {
> sprintf(mds[i], "%s", (char*)mdc[i]);


- mds[i] 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[i], "%d", mdc[i]);



> }
>
> 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


 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-27-2007

"Frank Liebelt" <ecosys_@hotmail.de> 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,10 0,49,53};
>
> for(i=0; i < 32; i++) {
> sprintf(mds[i], "%s", (char*)mdc[i]);

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

mds[i]=(char)mds[i]; /* 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[i] 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[i] 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


 
Reply With Quote
 
Frank Liebelt
Guest
Posts: n/a
 
      09-27-2007
@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
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-27-2007
"Frank Liebelt" <ecosys_@hotmail.de> 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'


 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      09-27-2007
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,10 0,49,53};
>
> for(i=0; i < 32; i++) {
> sprintf(mds[i], "%s", (char*)mdc[i]);

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[i] = mdc[i]; }, 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.

 
Reply With Quote
 
Frank Liebelt
Guest
Posts: n/a
 
      09-27-2007
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
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-27-2007
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,10 0,49,53};
>
> for(i=0; i < 32; i++) {
> sprintf(mds[i], "%s", (char*)mdc[i]);


mds[i] 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[i] 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[i] = mdc[i];
}
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
int urldecode(char *src, char *last, char *dest) gert C Programming 20 02-16-2007 11:28 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
newbie: char* int and char *int trey C Programming 7 09-10-2003 03:24 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57