problems with copying/editing strings

Discussion in 'C Programming' started by Lafer, Feb 18, 2004.

  1. Lafer

    Lafer Guest

    Hello,

    I am attempting to write a terminal interface program using PowerPC
    Assembly/C, where I am using an integer-to-ASCII conversion algorithm.
    Unfortunately, I have run into some difficulties that seem to deal
    with the syntax/character types that am I using. Below is a segment of
    my code which is intended to convert an integer to an array of ASCII
    digits:

    > > char * inttoascii(int val)
    > > {
    > > int i, temp1, temp2;
    > > i = 0;
    > > char * str;
    > > temp1 = 1;
    > > if(val < 0)
    > > {
    > > str = '-';
    > > val = val*-1;
    > > i++;
    > > }
    > > while(val > temp1)
    > > {
    > > temp1 = temp1*10;
    > > }
    > > while(temp1 >= 10)
    > > {
    > > temp1 = temp1/10;
    > > temp2 = val/temp1;
    > > str = temp2+48; *****
    > > i++;
    > > }
    > > str = '\0';
    > > return(str);
    > > }


    The ***** line is where I'm having my problems; presumably adding 48
    to a string element (i.e., a character) should convert that value to
    its ASCII equivalent so it can be displayed on my terminal screen, but
    when debugging the problem line doesn't seem to be doing anything at
    all; presumably this is an issue of syntax that you can help me with.
    I have also tried '0' and '0x30' in place of 48.

    Another problem I have is with copying strings into other strings, for
    example I have a help message that says:

    strcopy(output, "+ for add, - for subtract, ? for help");

    where strcopy is the following function:

    > > void strcopy(char b[], char a[])
    > > {
    > > int i;
    > >
    > > for (i = 0; a != '\0'; i++)
    > > b = a;
    > > b = '\0';
    > > }


    When I try to do this conversion it gives me a machine check exception
    in the simulator I'm using (SingleStep) something about error, memory
    access in 0x0000000D) which it doesn't do if I use a normal strcopy
    syntax where a and b are both declared as character pointers e.g. char
    *a, char *b. Note that I do not have the standard library, standard
    i/o or any other such libraries at my disposal for this program.

    If anyone wants to see more of the code or can help me out, please
    post a message here or e-mail me. Thanks!

    Matt Lafer
    University of Michigan
    Lafer, Feb 18, 2004
    #1
    1. Advertising

  2. Lafer

    -berlin.de Guest

    Lafer <> wrote:
    > I am attempting to write a terminal interface program using PowerPC
    > Assembly/C, where I am using an integer-to-ASCII conversion algorithm.
    > Unfortunately, I have run into some difficulties that seem to deal
    > with the syntax/character types that am I using. Below is a segment of
    > my code which is intended to convert an integer to an array of ASCII
    > digits:


    >> > char * inttoascii(int val)
    >> > {
    >> > int i, temp1, temp2;
    >> > i = 0;
    >> > char * str;
    >> > temp1 = 1;
    >> > if(val < 0)
    >> > {
    >> > str = '-';


    You have some bad bug here: str has never been initialized, so it
    probably points to some random place in memory. You have to allocate
    enough memory and assign that to str before you can start to use it.
    (There was another thread today about the question how long the
    string can become in the worst case, i.e. how much memory you need
    to allocate to be on the safe side.)

    >> > val = val*-1;
    >> > i++;
    >> > }
    >> > while(val > temp1)
    >> > {
    >> > temp1 = temp1*10;
    >> > }


    Lets assume that val is e.g. is 17. Then temp1 is now 100.

    >> > while(temp1 >= 10)
    >> > {
    >> > temp1 = temp1/10;


    First time round temp1 is 10, thesecond time it's 1.

    >> > temp2 = val/temp1;


    The first time round temp2 will be 1, the second time it's going
    to be 17, which you probably don't want. You need another line
    where you do

    val %= temp1;

    so that you get temp2 set to 7 on the second time through the loop.

    >> > str = temp2+48; *****


    Making sure that this works also with non-ASCII representations is
    extremely simple and doesn't cost you anything but just makes your
    program easier to read. Use

    str[ i ] = temp2 + '0'.

    (of course only after having assigned enough memory to str).

    >> > i++;
    >> > }
    >> > str = '\0';
    >> > return(str);


    Now here's another possible pitfall. If you should make str an
    automatic array (instead of allocating memory for the pointer or
    making it a static array) you would return a value that goes
    out of scope the moment you leave the function and thus can't be
    used by the caller.

    >> > }


    > The ***** line is where I'm having my problems; presumably adding 48
    > to a string element (i.e., a character) should convert that value to
    > its ASCII equivalent so it can be displayed on my terminal screen, but
    > when debugging the problem line doesn't seem to be doing anything at
    > all; presumably this is an issue of syntax that you can help me with.
    > I have also tried '0' and '0x30' in place of 48.


    > Another problem I have is with copying strings into other strings, for
    > example I have a help message that says:


    > strcopy(output, "+ for add, - for subtract, ? for help");


    > where strcopy is the following function:


    >> > void strcopy(char b[], char a[])
    >> > {
    >> > int i;
    >> >
    >> > for (i = 0; a != '\0'; i++)
    >> > b = a;
    >> > b = '\0';
    >> > }


    > When I try to do this conversion it gives me a machine check exception
    > in the simulator I'm using (SingleStep) something about error, memory
    > access in 0x0000000D) which it doesn't do if I use a normal strcopy
    > syntax where a and b are both declared as character pointers e.g. char
    > *a, char *b. Note that I do not have the standard library, standard
    > i/o or any other such libraries at my disposal for this program.


    Looks a lot as if you pass an unitialized pointer to that function.
    If you do something like

    char *output;
    strcopy(output, "+ for add, - for subtract, ? for help");

    you're in for such an error because 'output' doesn't point to any
    memory you own. It might just by chance point to memory location
    0x0D (but don't count on that). BTW, why don't you use the classic
    construct

    void strcopy( char *b, const char *a )
    {
    while ( *b++ = *a++ )
    /* empty */ ;
    }

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Feb 18, 2004
    #2
    1. Advertising

  3. -berlin.de writes:

    > Lafer <> wrote:
    >> I am attempting to write a terminal interface program using PowerPC
    >> Assembly/C, where I am using an integer-to-ASCII conversion algorithm.
    >>
    >> str = temp2+48;

    >
    > Making sure that this works also with non-ASCII representations is
    > extremely simple and doesn't cost you anything but just makes your
    > program easier to read. Use
    >
    > str[ i ] = temp2 + '0'.


    Note that the OP says he wants to write an integer-to-ASCII converter,
    as opposed to an integer-to-text-in-the-host-encoding converter. If he
    really means that, 48 would in fact be correct and '0' wrong on
    non-ASCII systems.

    I don't find it obvious what the OP means. "Terminal interface program"
    could refer to a program which is supposed to run on a non-ASCII system,
    but interfaces with an ASCII terminal.


    --
    ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
    / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
    \ `-' `-'(. .)`-'
    `-. Debian, a variant of the GNU operating system. \_/
    Martin Dickopp, Feb 18, 2004
    #3
  4. Lafer

    -berlin.de Guest

    Martin Dickopp <> wrote:
    > -berlin.de writes:


    >> Lafer <> wrote:
    >>> I am attempting to write a terminal interface program using PowerPC
    >>> Assembly/C, where I am using an integer-to-ASCII conversion algorithm.
    >>>
    >>> str = temp2+48;

    >>
    >> Making sure that this works also with non-ASCII representations is
    >> extremely simple and doesn't cost you anything but just makes your
    >> program easier to read. Use
    >>
    >> str[ i ] = temp2 + '0'.


    > Note that the OP says he wants to write an integer-to-ASCII converter,
    > as opposed to an integer-to-text-in-the-host-encoding converter. If he
    > really means that, 48 would in fact be correct and '0' wrong on
    > non-ASCII systems.


    > I don't find it obvious what the OP means. "Terminal interface program"
    > could refer to a program which is supposed to run on a non-ASCII system,
    > but interfaces with an ASCII terminal.


    Yupp, of course you're right, I just carelessly assumed that the OP
    meant int-to-string conversion when s/he wrote integer-to-ASCII.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Feb 18, 2004
    #4
  5. Martin Dickopp <> scribbled the following:
    > -berlin.de writes:
    >> Lafer <> wrote:
    >>> I am attempting to write a terminal interface program using PowerPC
    >>> Assembly/C, where I am using an integer-to-ASCII conversion algorithm.
    >>>
    >>> str = temp2+48;

    >>
    >> Making sure that this works also with non-ASCII representations is
    >> extremely simple and doesn't cost you anything but just makes your
    >> program easier to read. Use
    >>
    >> str[ i ] = temp2 + '0'.


    > Note that the OP says he wants to write an integer-to-ASCII converter,
    > as opposed to an integer-to-text-in-the-host-encoding converter. If he
    > really means that, 48 would in fact be correct and '0' wrong on
    > non-ASCII systems.


    > I don't find it obvious what the OP means. "Terminal interface program"
    > could refer to a program which is supposed to run on a non-ASCII system,
    > but interfaces with an ASCII terminal.


    He said "integer-to-ASCII" but who's to say he didn't mean "integer-
    to-whatever-happens-to-be-this-platform's-encoding"? The latter would
    be more useful in most common situations. Getting these muddled up can
    be the result of an "all the world's ASCII" viewpoint.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Immanuel Kant but Genghis Khan."
    - The Official Graffitist's Handbook
    Joona I Palaste, Feb 19, 2004
    #5
    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. Mark van Heeswijk

    copying strings efficiently?

    Mark van Heeswijk, Aug 29, 2003, in forum: C++
    Replies:
    3
    Views:
    368
    John Harrison
    Aug 31, 2003
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    740
    Malcolm
    Jun 24, 2006
  3. Replies:
    0
    Views:
    577
  4. Khookie

    Copying strings

    Khookie, Dec 14, 2007, in forum: C Programming
    Replies:
    6
    Views:
    317
    Chris Torek
    Dec 14, 2007
  5. Angus

    confused about copying strings

    Angus, Jul 17, 2010, in forum: C Programming
    Replies:
    4
    Views:
    274
    Thomas Jollans
    Jul 18, 2010
Loading...

Share This Page