Access Violation

This is a discussion on Access Violation within the C Programming forums, part of the General Programming Boards category; Hi there, I am very new to programming and I am currently going through some material provided by my course ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Access Violation

    Hi there, I am very new to programming and I am currently going through some material provided by my course lecturer. I am trying to learn about data structures with the code below. It compiles fine but when I try to run it, I get an Access Violation message. I'm using CC386 IDE. Any ideas as to why I am getting this message? Any help will be greatly appreciated.


    Code:
    #include <string.h>
    
    typedef struct{  
    	unsigned char Type[20];
    	unsigned int Next, Previous;
    
    }Shape;
    
    Shape MyShape[3];
    
    void main(){
    
    	strcpy(MyShape[0].Type ,"Square");				//Define a Square Shape
    	MyShape[0].Next=1;
    	MyShape[0].Previous=2;
    	
    	strcpy(MyShape[1].Type ,"Triangle");			//Define a Triangle Shape
    	MyShape[1].Next=2;
    	MyShape[1].Previous=0;
    	
    	strcpy(MyShape[2].Type ,"Circle");				//Define a Circle Shape
    	MyShape[2].Next=0;
    	MyShape[2].Previous=1;
    	
    	int n,p,c;
    	while(1){										//Loop forever
    				p=MyShape[c].Previous;				//Read current Shape's previous neighbour
    				n=MyShape[c].Next;					//Read current Shape's next neighbour
    													//Print current, previous and next shape
    				printf("\n\n%sis followed by %s and preceded by %s",MyShape[n].Type, MyShape[p].Type, MyShape[c].Type);
    													
    				c++;if(c>2) c=0;					//Reset counter at 3
    				for(long d=0;d<5e8;d++);			//Delay output
    			}
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    'c' is not initialized to anything before you use it as an index in the while(1) loop.

    When in doubt, comment out code until you find the line(s) that blow it up.

    Plus...
    Code:
    	MyShape[2].Previous=1;
    	
    	int n,p,c;
    	while(1){										//Loop forever
    .
    .
    .
            for(long d=0;d<5e8;d++);
    While it may be valid to declare variables all over the place, IMO it's cleaner and easier to detect errors if they are all declared at the top.
    Last edited by mike65535; 04-15-2011 at 01:01 PM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Thank you for your response.
    Sorry for the incredibly late reply, I've been on holiday

    It turns out that the software didn't like my laptop for some reason! I just did my project at uni in the end.
    Thanks again, much appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Access Violation
    By beene in forum Game Programming
    Replies: 8
    Last Post: 03-27-2007, 01:39 PM
  3. access violation
    By bonkey in forum C++ Programming
    Replies: 15
    Last Post: 11-20-2003, 09:22 AM
  4. Access Violation!!
    By Yoshi in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2002, 01:22 PM

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