Thread: What was wrong with this code??

  1. #16
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I suggest you use Quzah's suggestion for debugging. printf() is a lot easier to use than GDB.

  2. #17
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Here is an example that might be a little more pertinant to you:

    Code:
    int a, c, b = 10;
    char array[10];
    for(a = 0; a <= b; a++) {
        c = array[a];
    }
    The above code will probably give you a seg fault. If you add in a printf() statement like so:
    Code:
    int a, c, b = 10;
    char array[10];
    for(a = 0; a <= b; a++) {
        printf("About to access index %d\n",a);
        c = array[a];
    }
    You will (hopefully) get the seg fault before it prints "About to access index 10". This helps you to realize that you are accessing the array outside its bounds.

  3. #18
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Using quzah's example you're guaranteed that the debugging message will be shown because of the fflush() call.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM