Thread: writing to a structure via pointer crashes...

  1. #1
    Unregistered
    Guest

    writing to a structure via pointer crashes...

    Hi,
    i don't get why the following code crashes, please help!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    struct fifos
    {
        int *buf, in_cur;
    };
    
    typedef struct fifos fifo;
    
    void makefifo( fifo *fif, int amount )
    {
    
        fif->buf = ( int* )malloc( amount );
        fif->in_cur = 0;
    }
    
    main()
    {
        fifo *newfifo;
        makefifo( newfifo, 200 );
    }
    makefifo is supposed to create a fifo-stack with "amount" bytes, through a pointer to "newfifo"...

  2. #2
    Unregistered
    Guest
    ah, i'm stupid.
    Code:
    fifo newfifo;
    makefifo( &newfifo, 200 );
    ..is what it should look like.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Traditionally, this kinda thing would be done with malloc...
    Code:
    fifo * makefifo(int amount )
    {
        fifo * fif;
        fif = malloc (sizeof (fifo));
        fif->buf = ( int* )malloc( amount );
        fif->in_cur = 0;
        return fif;
    }
    
    main ()
    {
     fifo * newfifo;
     newfifo = makefifo (200);
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Unregistered
    Guest
    I thought about your version before, but i decided to take mine and i'm not going to change it for any "tradition" ;).
    Thanks anyway

  5. #5
    Unregistered
    Guest
    You may also want to allocate some memory for the fifos pointer...

    if you don't know why then you need to study more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to structure question
    By matthughes in forum C Programming
    Replies: 8
    Last Post: 05-19-2007, 01:07 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. structure pointer need more info
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-18-2001, 05:41 PM
  5. Pointer to next Structure
    By Garfield in forum C Programming
    Replies: 6
    Last Post: 09-16-2001, 03:18 PM