What was wrong with this code??

This is a discussion on What was wrong with this code?? within the C Programming forums, part of the General Programming Boards category; I suggest you use Quzah's suggestion for debugging. printf() is a lot easier to use than GDB....

  1. #16
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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,246
    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,163
    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.

Page 2 of 2 FirstFirst 12
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, 09: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, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01: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

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