All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Mikael Aronsson" <mikael-aronsson@telia.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: convert INT to CHAR but print's BEEP...
Date: Wed, 12 Nov 2003 08:11:28 +0100	[thread overview]
Message-ID: <00ab01c3a8ec$311abd10$bf95d1d9@PC128> (raw)
In-Reply-To: Pine.LNX.4.21.0311120519210.31743-100000@hestia

Hi !

The problem with strcat is that you cannot just use a pointer to the
character because all C string functions require that you end the string
with a zero (0) byte.

You could do something like this for eample:
char temp[ 2];
temp[ 0] = my_character;
temp[ 1] = '\0';
strcat( org_string, temp);

Mikael

----- Original Message ----- 
From: "J." <mailing-lists@xs4all.nl>
To: <linux-c-programming@vger.kernel.org>
Sent: Wednesday, November 12, 2003 5:35 AM
Subject: convert INT to CHAR but print's BEEP...


> Wednesday, November 12 05:19:21
>
> Hello, I have kind of a problem with converting an int to a char.
> The get_rand_str() function returns an string build of random
> charaters with a max length of int `MAX'.
>
> This seems to work at first glance:
> ~: ./program
> qqxfrd
>
> However if I examen the output closer:
> ~: ./program | od -a
> 0000000   q   q soh   x stx   f etx   r eot   d enq  nl
> 0000014
>
> Now I know why my computer keeps beeping everytime it
> outputs a string :-)
>
> What is the correct way of changing the int value to a
> char value so that I can append it to the return string ?
>
> char *get_rand_str(int max) {
>  int i = 0;
>  char value;
>  char *retval = NULL;
>
>  for(; i < max; i++) {
>   value = get_ascii_code(97, 122);
>   retval = (char *)realloc(retval, sizeof(char));
>   strcat(retval, &value);
>  }
>
>  return retval;
>  free(retval);
> }
>
> int get_ascii_code(int low, int high) {
>  int k = 0;
>  double d = 0;
>
>  d = (double)rand() / ((double)RAND_MAX + 1);
>  k = (int)(d * (high - low + 1));
>  return(low + k);
> }
>
> Thank you....
>
> J.
>
> --
> KonkyFong
>
> -
> To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


  reply	other threads:[~2003-11-12  7:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-08  9:30 mixing C/C++ Elias Athanasopoulos
2003-11-08 13:36 ` James Stevenson
2003-11-10 15:00 ` Matthew Studley
2003-11-10 17:37   ` Elias Athanasopoulos
2003-11-12  4:35     ` convert INT to CHAR but print's BEEP J.
2003-11-12  7:11       ` Mikael Aronsson [this message]
2003-11-13  1:26         ` convert INT to CHAR - SOLVED J.
2003-11-13  6:36       ` convert INT to CHAR but print's BEEP Jeff Woods

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='00ab01c3a8ec$311abd10$bf95d1d9@PC128' \
    --to=mikael-aronsson@telia.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.