Print a struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arnuld

    Print a struct

    I am unable to add anything to this structI 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);

    }
    }





    --

    my email is @ the above blog.


  • LoYwEnG

    #2
    Re: Print a struct

    On Nov 7, 9:28 pm, arnuld <sunr...@invali d.addresswrote:
    I am unable to add anything to this structI 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.

    Comment

    • Martien Verbruggen

      #3
      Re: Print a struct

      On Fri, 07 Nov 2008 18:28:50 +0500,
      arnuld <sunrise@invali d.addresswrote:
      I am unable to add anything to this structI 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?

      Comment

      • Lew Pitcher

        #4
        Re: Print a struct

        On November 7, 2008 08:28, in comp.lang.c, arnuld (sunrise@invali d.address)
        wrote:
        I am unable to add anything to this structI 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. ------


        Comment

        • santoshsy

          #5
          Re: Print a struct

          On Nov 7, 6:28 pm, arnuld <sunr...@invali d.addresswrote:
          I am unable to add anything to this structI 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(s truct 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.

          Comment

          • William Pursell

            #6
            Re: Print a struct

            On 11 Nov, 05:54, santoshsy <santos...@gmai l.comwrote:
                my_combo = malloc(sizeof(s truct macd));   // Allocating Memory
            Better is:

            my_combo = malloc( sizeof *my_combo );

            Although in this case it is probably best to declare
            an actual struct:

            struct macd foo;
            struct macd *my_combo;
            my_combo = &foo;

            Or even just do:

            struct macd my_combo[ 1 ];

            Comment

            • William Pursell

              #7
              Re: Print a struct

              On 7 Nov, 13:59, Lew Pitcher <lpitc...@teksa vvy.comwrote:
              On November 7, 2008 08:28, in comp.lang.c, arnuld (sunr...@invali d.address)
              wrote:
                struct macd* my_combo = NULL;
                my_combo->price = 100;
              <snip>
              >
              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?
              >
              It went to nowhere plus offsetof( struct macd, price ), of course! :)

              Comment

              Working...