Program segfaults when trying to print non-null integer type value

This is a discussion on Program segfaults when trying to print non-null integer type value within the C Programming forums, part of the General Programming Boards category; This is retarded i have no idea why this is happening. Code: if(active->ptr->threadId == NULL) printf("null actve\n"); else printf("not null\n") ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Program segfaults when trying to print non-null integer type value

    This is retarded i have no idea why this is happening.

    Code:
    if(active->ptr->threadId == NULL)
        printf("null actve\n");
    else 
        printf("not null\n")
    printf("id: %d\n", (int)(*(active->ptr->threadId)));
    and the programs segfaults on the printf. the size of threadId is equal to integer size. threadId is a pointer.
    by the way it always prints not null.
    Last edited by foolfoolz; 10-30-2009 at 05:53 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Ill edit your code to show whats happening:
    Code:
    if(active->ptr->threadId == NULL)
    {
        printf("null actve\n");
    }
    else
    {
        printf("not null\n")
    }
    
    //always gets executed, including when the pointer is null
    printf("id: %d\n", (int)(*(active->ptr->threadId)));
    Its best practice to always surround these blocks of code (if, while, etc) with {}, even if the content is one line.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    the pointer is never null.

  4. #4
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    missing a semicolon there after the printf

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    D'oh, good catch Epy.

    foolfoolz, why are you posting code different from what you are running? You obviously arent running this code, because its syntactically incorrect and therefore cannot be compiled. Make the changes suggested above, recompile, and test again. If problems persist, post your actual code, and give more context, as well as any other useful information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  5. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

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