access violation!!

This is a discussion on access violation!! within the C Programming forums, part of the General Programming Boards category; Code: void main(void) { char what[24]; fgets(what,24)); int base; base = atoi(what); char * address = base; while(1) printf("%i", (char)*address++); ...

  1. #1
    accviol
    Guest

    Angry access violation!!

    Code:
    void main(void) 
     {
      char what[24];
      fgets(what,24));
      int base;   base = atoi(what);
      char * address = base;
        while(1) printf("%i", (char)*address++);
      return;
      }

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    char * address = base;
    this is pointing to the converted int, proabally some ram already used. you want to add the '&' sign to base so it points to where it is and not what it holds.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>void main( void )

    BADBADBADBADBADBADBADBAD!!!!

    Use int main( void ) and return 0;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: access violation!!

    This code wouldn't compile.

    1) This line is wrong:

    void main(void)

    2) This is wrong:
    Code:
      char what[24];
      fgets( what,24 ) );
      int base;   base = atoi(what);
    Do you even listen to your compiler? You have two ')' there when you should just have one.

    Furthermore, this is C, not C++. As such, you cannot radomly declare new variables wherever you like, unless by some odd chance, you happen to have a C99 compatible compiler.

    3) This is also wrong, as described in the first post.

    char * address = base;

    4) What are you doing?

    while(1) printf("%i", (char)*address++);

    This code will print out characters (as numbers) forever and ever and ever and ever. And why are you typcasting here? Use %c if you want characters, otherwise, lose the cast. My guess is that you're trying to do something your compiler is warning about, so you're trying to hide the warning.

    The above line is also a potential cause of access violation, because of your foolish while loop.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: access violation!!

    Also
    fgets(what,24, stdin);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  2. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  3. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  4. Strange access violation
    By jimmy_anttila in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2004, 03:10 AM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 01:46 PM

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