Print a struct

Discussion in 'C Programming' started by arnuld, Nov 7, 2008.

  1. arnuld

    arnuld Guest

    I am unable to add anything to this struct> I can't understand why I am
    getting a Segfault:



    #include <stdio.h>


    struct macd
    {
    int price;
    };


    void print_combo( struct macd* );


    int main(void)
    {
    struct macd* my_combo = NULL;

    my_combo->price = 100;


    return 0;
    }


    void print_combo( struct macd* mc )
    {
    if( mc )
    {
    printf("Price = %d\n", mc->price);

    }
    }





    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
     
    arnuld, Nov 7, 2008
    #1
    1. Advertising

  2. arnuld

    LoYwEnG Guest

    On Nov 7, 9:28 pm, arnuld <> wrote:
    > I am unable to add anything to this struct> I can't understand why I am
    > getting a Segfault:
    >
    > #include <stdio.h>
    >
    > struct macd
    > {
    >   int price;
    >
    > };
    >
    > void print_combo( struct macd* );
    >
    > int main(void)
    > {
    >   struct macd* my_combo = NULL;

    Maybe because of this statement.
    >
    >   my_combo->price = 100;
    >
    >   return 0;
    >
    > }
    >
    > void print_combo( struct macd* mc )
    > {
    >   if( mc )
    >     {
    >       printf("Price = %d\n", mc->price);
    >
    >     }
    >
    > }
    >
    > --www.lispmachine.wordpress.com
    > my email is @ the above blog.
     
    LoYwEnG, Nov 7, 2008
    #2
    1. Advertising

  3. On Fri, 07 Nov 2008 18:28:50 +0500,
    arnuld <> wrote:
    > I am unable to add anything to this struct> I can't understand why I am
    > getting a Segfault:


    > struct macd
    > {
    > int price;
    > };


    > int main(void)
    > {
    > struct macd* my_combo = NULL;


    This is not a struct. It's a pointer to a struct. And it doesn't point
    to anywhere where there could be a struct.

    > my_combo->price = 100;


    You're trying to dereference the pointer that you just set to NULL
    yourself. You can't do that. Either allocate memory with malloc, or use
    a struct.


    struct macd *my_combo ;
    macd = malloc(sizeof *macd);
    my_combo->price = 100;

    OR

    struct macd my_combo;
    my_combo.price = 100;

    Just like any other pointer, a struct pointer needs to point to
    some memory.

    Martien
    --
    |
    Martien Verbruggen | Since light travels faster than sound, is
    | that why some people appear bright until you
    | hear them speak?
     
    Martien Verbruggen, Nov 7, 2008
    #3
  4. arnuld

    Lew Pitcher Guest

    On November 7, 2008 08:28, in comp.lang.c, arnuld ()
    wrote:

    > I am unable to add anything to this struct> I can't understand why I am
    > getting a Segfault:
    >
    >
    >
    > #include <stdio.h>
    >
    >
    > struct macd
    > {
    > int price;
    > };
    >
    >
    > void print_combo( struct macd* );
    >
    >
    > int main(void)
    > {
    > struct macd* my_combo = NULL;


    my_combo is a pointer to a structure. The pointer is set to NULL. There is
    no space allocated to the structure.

    > my_combo->price = 100;


    Here, you put a value into the structure. Where did this value wind up?
    Remember, at this point, the structure only exists in potential, and /not/
    as an object that can be modified. So, what did you modify?

    Remember also that my_combo is a pointer, which (at the moment) points to
    (the official) nowhere. So, again, where did your value of 100 go?

    >
    > return 0;
    > }

    [snip]

    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Nov 7, 2008
    #4
  5. arnuld

    santoshsy Guest

    On Nov 7, 6:28 pm, arnuld <> wrote:
    > I am unable to add anything to this struct> I can't understand why I am
    > getting a Segfault:
    >
    > #include <stdio.h>
    >
    > struct macd
    > {
    >   int price;
    >
    > };
    >
    > void print_combo( struct macd* );
    >
    > int main(void)
    > {
    >   struct macd* my_combo = NULL;
    >



    my_combo = malloc(sizeof(struct macd)); // Allocating Memory

    This should work fine.






    >   my_combo->price = 100;
    >
    >   return 0;
    >
    > }
    >
    > void print_combo( struct macd* mc )
    > {
    >   if( mc )
    >     {
    >       printf("Price = %d\n", mc->price);
    >
    >     }
    >
    > }
    >
    > --www.lispmachine.wordpress.com
    > my email is @ the above blog.
     
    santoshsy, Nov 11, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    404
    RA Scheltema
    Jan 6, 2004
  2. Gunnar G

    struct in struct

    Gunnar G, May 31, 2004, in forum: C++
    Replies:
    14
    Views:
    805
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  4. keto
    Replies:
    0
    Views:
    956
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    358
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page