Is this bad?

This is a discussion on Is this bad? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void change_x(int **x) { *x = (int *)10; } int main(void) { int *x = NULL; change_x(&x); ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    29

    Is this bad?

    Code:
    #include <stdio.h>
    
    void change_x(int **x)
    {
        *x = (int *)10;
    }
    
    int main(void)
    {
        int *x = NULL;
        
        change_x(&x);
        
        printf("%d\n", x);
        
        return 0;
    }
    I'll be using a linked list in my next stupid program and I don't want to use any global variables. This was my practice
    Is this correct? gcc didn' t give me any errors or warnings. Thanks.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    printf("%d\n", x);
    ^---- bad

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    That code is bad in general. You have x declared as a pointer to an integer, and it's pointer value is modified by the function change_x. A logical assumption would be that the programmer could then dereference the value of x, but in this case it would crash the program. Why not keep it simple and just do:

    Code:
    #include <stdio.h>
    
    void change_x(int *x)
    {
        *x = 10;
    }
    
    int main(void)
    {
        int x = 0;
        
        change_x(&x);
        
        printf("%d\n", x);
        
        return 0;
    }
    I'm not sure what your code has to do with a linked list, so maybe I'm missing the point of this little exercise.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > I'll be using a linked list in my next stupid program and I don't want to use any global variables
    Well you also need to make sure you don't use any casts either.
    Because indiscriminate casting like that cripples the compiler's ability to tell you anything useful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    I want to write an add_node function that will not depend on a global variable head.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    I want to write an add_node function that will not depend on a global variable head.
    Well, head need not be global, you can always pass head to your add_node function.

    Sahil

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I guess I understand your problem
    Code:
    void add_node(node **head ){
        node * newnode = malloc(sizeof( node ) );
        if ( *head == 0 ) {
              *head = newnode;
        }
        else 
           .....
    }
    
    int main() {
       node * head;
       head = 0;
       add_node( &head );  // head will be initialized in add_node
       return 0;
    }
    And yes that would work.

    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bad and fail of steam
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 02-19-2008, 02:07 AM
  2. Can you still view the bios screen with a bad CPU?
    By HyperCreep in forum Tech Board
    Replies: 4
    Last Post: 12-31-2006, 05:57 PM
  3. data loss bad bad bad
    By RoD in forum Tech Board
    Replies: 4
    Last Post: 05-01-2003, 12:06 PM
  4. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 07:52 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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