Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: C mixes decimals with chars in array

  1. #1
    Join Date
    Feb 2007
    Location
    NZ
    Beans
    89
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    C mixes decimals with chars in array

    I'm creating a programme that finds the shortest route between two points H and S in a maze. such as the one shown.
    Code:
    #####
    #H# . S
    # . # . #
    # .  .  . #
    #####
    I'm doing it by finding the H, changing it to a zero and systematically changing the dots to decimals that increase further away from the H.

    Code:
    #####
    #H#6S
    #1# 5#
    #2 3 4#
    #####
    This works for small mazes but when paths become larger than 35 steps my programme confuses the # with the number 35 and everything becomes a mess. Is there a way to get C to look at a item in an array and tell the difference between the char "#" and the decimal "35"?

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: C mixes decimals with chars in array

    No. The character '#' and the integer 35 are the same thing. You need to reconsider your design completely.
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Feb 2007
    Location
    NZ
    Beans
    89
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: C mixes decimals with chars in array

    Then are there any chars that do not have a decimal value that I could convert the #'s and S to?

  4. #4
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: C mixes decimals with chars in array

    No. The char type is an integer type. Any character is represented internally as an integer,
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Feb 2007
    Location
    NZ
    Beans
    89
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: C mixes decimals with chars in array

    ok thanks.

  6. #6
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: C mixes decimals with chars in array

    If you want to do this in C you will need to distinguish between the displayed maze and the maze in which you will do your computations. For example you coud make the maze an array of ints, not of chars. Then you would give the "walls" a value of -1, and the problem is solved. And you would have a display function that would display a # for the -1 values. Of course you need to write the display function yourself, you can't use any of the standard functions.

    I believe I told you that in your previous thread already...
    「明後日の夕方には帰ってるからね。」


  7. #7
    Join Date
    Aug 2012
    Beans
    32

    Re: C mixes decimals with chars in array

    Quote Originally Posted by scorpious View Post
    Then are there any chars that do not have a decimal value that I could convert the #'s and S to?
    To get what you want you'll have to use C++ where character literals are actually characters.

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: C mixes decimals with chars in array

    Quote Originally Posted by durdenstationer View Post
    To get what you want you'll have to use C++ where character literals are actually characters.
    Huh? How's it any different from C?

    All characters can be interpreted as integers; the converse is not true.

    For reference... read the man-page for 'ascii'.

    P.S. Perhaps you meant wide-characters?
    Last edited by dwhitney67; August 14th, 2012 at 02:30 PM.

  9. #9
    Join Date
    Aug 2012
    Beans
    32

    Re: C mixes decimals with chars in array

    Quote Originally Posted by dwhitney67 View Post
    Huh? How's it any different from C?
    It's different in the way I and the other person above mentioned. Character literals are treated as the integer value of the literal in C and as the actual character in C++. This is the reason why sizeof 'A' will give you different results in C and C++. This is pretty basic knowledge.

    Quote Originally Posted by dwhitney67 View Post
    P.S. Perhaps you meant wide-characters?
    No, I didn't mean that at all. I meant exactly what I said.
    Last edited by durdenstationer; August 14th, 2012 at 02:45 PM.

  10. #10
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: C mixes decimals with chars in array

    Quote Originally Posted by durdenstationer View Post
    This is the reason why sizeof 'A' will give you different results in C and C++. This is pretty basic knowledge.
    This is news to me. It seems that 'A' is interpreted as an int. I didn't know that.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •