How to access pointer inside a structure pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohammadthalif
    New Member
    • Aug 2007
    • 9

    How to access pointer inside a structure pointer

    Hello friends,

    struct datas{
    int *a,*b;
    } *name;

    can you explain me how can i get the data at location pointed by *a and *b.

    i tried,

    name->*a = 10; // but this produces an error...
    name->*b = 5; // but this produces an error...

    Thanks...
    H.Mohamed Thalib
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    I'll move your question to the C/C++ forum section where it belongs.

    kind regards,

    Jos

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by mohammadthalif
      Hello friends,

      struct datas{
      int *a,*b;
      } *name;

      can you explain me how can i get the data at location pointed by *a and *b.

      i tried,

      name->*a = 10; // but this produces an error...
      name->*b = 5; // but this produces an error...

      Thanks...
      H.Mohamed Thalib
      You need to dereference it in front:
      [code=cpp]
      *name->a = 10;
      [/code]
      Use parenthesis to make it clearer:
      [code=cpp]
      *(name->a) = 10;
      [/code]

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Even more important: a pointer must point to something; in the OPs example
        the name pointer doesn't point to a struct with two int pointers so nothing can
        be dereferenced at all.

        kind regards,

        Jos

        Comment

        • mohammadthalif
          New Member
          • Aug 2007
          • 9

          #5
          Originally posted by ilikepython
          You need to dereference it in front:
          [code=cpp]
          *name->a = 10;
          [/code]
          Use parenthesis to make it clearer:
          [code=cpp]
          *(name->a) = 10;
          [/code]

          Thank you guys ... i t works,....

          Comment

          • sajet
            New Member
            • Nov 2008
            • 3

            #6
            try this

            name = (struct datas *)malloc(sizeof *name);

            now u can access

            name->a = 10;
            name->b = 5;

            Comment

            • TamusJRoyce
              New Member
              • Apr 2008
              • 108

              #7
              Close Sajet... But your allocation only allocates space for an unsigned integer/pointer, since name is a pointer. You need the sizeof(datas).

              name = (struct datas *)malloc(sizeof (datas))

              Now that you have a valid datas allocated to name, you need *a and *b to be something.

              // Always check to see if name truely has been allocated.
              // The system could be out of memory.
              if (name != NULL)
              {
              name->a = (int)malloc(siz eof(int))
              name->b = (int)malloc(siz eof(int))

              if (name->a != NULL)
              {
              *name->a = 5

              // Done using it, so lets free it
              free(name->a)
              } // End if

              if (name->b != NULL)
              {
              *name->b = 10

              // Done using it, so lets free it
              free(name->b)
              } // End if

              // Done using it, so lets free it
              free(name)
              } // End if

              Search how pointers work with the below if you're interested...
              1. Dynamic Arrays
              2. Polymorphism

              1. But dynamic memory allocation is exciting/useful when you get into linked lists and dynamic arrays.

              2. It's also useful in C++ when dynamically allocating a class which inherits a base class (using new/delete instead of malloc/free, of course), and that class can be set to a base class pointer with that class abstractly performing like the base class, but doing much more stuff in the extended part.

              Hopefully helpful,

              TamusJRoyce

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by TamusJRoyce
                Close Sajet... But your allocation only allocates space for an unsigned integer/pointer, since name is a pointer. You need the sizeof(datas).
                Not true notice Sajet wrote

                name = (struct datas *)malloc(sizeof *name);

                not

                name = (struct datas *)malloc(sizeof name);

                since *name is a struct of type datas sizeof *name returns the size of the struct name points to. In fact for me

                name = malloc(sizeof *name);

                is preferable to

                name = malloc(sizeof (datas));

                because if the type of name is changed (which I admit is notterribly likely) the first form of the malloc still works without alteration where as the second requires a change to the sizeof to work correctly. For any type T using

                T *pointer;
                pointer = malloc(sizeof *pointer);

                is about the most robust way you can write the malloc. NOTE I have removed the casts because they are not necessary (in fact they are dangerous) in C and if you are using C++ you should be using new anyway to ensure and constructors get called.

                Comment

                Working...