atoi produces segfault

This is a discussion on atoi produces segfault within the C Programming forums, part of the General Programming Boards category; anyone know why atoi produces a segfault? I want to convert '\xFF' to 255 integer. Heres the code: Code: printf("%d",atoi('\xFF'));...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    atoi produces segfault

    anyone know why atoi produces a segfault? I want to convert '\xFF' to 255 integer. Heres the code:

    Code:
    printf("%d",atoi('\xFF'));

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For one thing single quotes are around character, you need double quotes for string literals.
    Another thing is that atoi might not be able to convert hex strings, you might try strtol.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Why don't you just write it like this?
    Code:
    printf("%d",0xFF);
    I don't see the problem, compiler will always convert octal and hexadecimal values into decimals at compile time. If you want to print values in hexadecimal format than you should use %x.
    Code:
    printf("%x",0xFF);
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by hauzer View Post
    Why don't you just write it like this?
    Code:
    printf("%d",0xFF);
    I don't see the problem, compiler will always convert octal and hexadecimal values into decimals at compile time. If you want to print values in hexadecimal format than you should use %x.
    Code:
    printf("%x",0xFF);

    I cant do that because im really doing something like this:

    Code:
    if (myarray[*mystruct.myval] == '\xFF') { ...code}
    NOTE: mystruct is just a struct and myval is a pointer to a character... i need to turn it into an int to access the value in myarray

    any ideas?
    Last edited by someprogr; 11-04-2008 at 10:22 AM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    But... but... '\xFF' IS the number 255.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by tabstop View Post
    But... but... '\xFF' IS the number 255.
    right, so i would be accessing myarray[255]... thats what i want... i need to convert the char to an int so i can access the array

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Never mind the update to the O/P's post is what I was after.
    Maybe an extra pair of parenthesis if I understand what you are after or not...
    Code:
    if (myarray([*mystruct.myval] == '\xFF')) { ...code}
    Last edited by itCbitC; 11-04-2008 at 10:36 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by someprogr View Post
    right, so i would be accessing myarray[255]... thats what i want
    You say this as though myarray[(unsigned char)'\xFF'] doesn't work or something.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by tabstop View Post
    You say this as though myarray[(unsigned char)'\xFF'] doesn't work or something.

    Code:
    if (myarray[*mystruct.myval] == '\xFF') { ...code}
    does not work when myval = 255

    however....

    Code:
    if (myarray[255] == '\xFF') { ...code}
    works.... Why?

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Back-to-Basics show declaration and initialization of mystruct.
    and are you comparing the array index or the value stored at myarray[255]??

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Is mystruct.myval a pointer?
    More importantly, do you get any warnings? Did you try enabling maximum warnings on your compiler?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by itCbitC View Post
    Back-to-Basics show declaration and initialization of mystruct.
    and are you comparing the array index or the value stored at myarray[255]??
    Ok... I found something out:

    myval in mystruct = \xD8... however when it is getting passed over and casted to an (int) it now = -40.... I need it to equal its true value of 216... How can I do this?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's why I cast to unsigned in my example. '\xD8' is -40 as signed, but 216 as unsigned.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by tabstop View Post
    That's why I cast to unsigned in my example. '\xD8' is -40 as signed, but 216 as unsigned.
    ahhhh that did it thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with atoi
    By NewbGuy in forum C Programming
    Replies: 2
    Last Post: 05-22-2009, 11:55 PM
  2. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  3. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  4. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 03:53 PM
  5. Problem with atoi in C
    By garagebrian in forum C Programming
    Replies: 5
    Last Post: 08-24-2004, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21