structure pointers

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

    #1

    structure pointers

    Hello

    This is Raghu. I have a problem in the pointers.
    The code is
    typedef struct Tlv{
    /*type Length value for each
    message */
    unsigned char ucType; /*Type varies from message to
    message */
    unsigned short usLength;/*Length */
    unsigned int ucValue;/*Value*/
    }tlv;


    typedef struct te{
    char d;
    int e;
    }Q;
    struct temp{
    int a;
    unsigned int c;
    Q *q;
    tlv *T;
    };
    struct temp t;
    t.a = 4;
    t.c = 31;
    t.q[0].d =21;
    t.q[0].e = 23;
    t.q[1].d = 11;
    t.q[1].e = 4;


    t.T -ucType = 1;
    t.T -usLength = 11;
    t.T -ucValue = 21;
    printf("The entered values are ");
    printf("%d\n%d\ n", t.a, t.c);
    printf(" %d \n", t.q[0].d);
    printf(" %d \n",t.T->ucType);
    return 0;
    }
    when I run this program some error is generating that is Segmentation
    fault.
    Can you please help me.
    Thanks in advance.

    -Bye
    Raghu

  • Richard Heathfield

    #2
    Re: structure pointers

    raghu said:
    Hello
    >
    This is Raghu. I have a problem in the pointers.
    The code is
    typedef struct Tlv{
    /*type Length value for each
    message */
    unsigned char ucType; /*Type varies from message to
    message */
    unsigned short usLength;/*Length */
    unsigned int ucValue;/*Value*/
    }tlv;
    >
    >
    typedef struct te{
    char d;
    int e;
    }Q;
    struct temp{
    int a;
    unsigned int c;
    Q *q;
    tlv *T;
    };
    struct temp t;
    t.a = 4;
    Here's your problem. You can't have executable code except inside a
    function.
    t.c = 31;
    This breaks for the same reason.
    t.q[0].d =21;
    This breaks for the same reason, but also because t.q doesn't point anywhere
    useful (or if it was statically initialised, it has the value NULL). Either
    way, t.q[0] is an illegal memory access.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • raghu

      #3
      Re: structure pointers


      Richard Heathfield wrote:
      raghu said:
      >
      Hello

      This is Raghu. I have a problem in the pointers.
      The code is
      typedef struct Tlv{
      /*type Length value for each
      message */
      unsigned char ucType; /*Type varies from message to
      message */
      unsigned short usLength;/*Length */
      unsigned int ucValue;/*Value*/
      }tlv;


      typedef struct te{
      char d;
      int e;
      }Q;
      struct temp{
      int a;
      unsigned int c;
      Q *q;
      tlv *T;
      };
      struct temp t;
      t.a = 4;
      >
      Here's your problem. You can't have executable code except inside a
      function.
      >
      t.c = 31;
      >
      This breaks for the same reason.
      >
      t.q[0].d =21;
      >
      This breaks for the same reason, but also because t.q doesn't point anywhere
      useful (or if it was statically initialised, it has the value NULL). Either
      way, t.q[0] is an illegal memory access.
      >
      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)
      But even I modified the code as follows the error is repeting.
      the code is
      main(){
      struct temp t;
      t.a = 4;
      t.c = 31;


      t.T -ucType = 1;
      t.T -usLength = 11;
      t.T -ucValue = 21;
      printf("The entered values are %d\n ", sizeof(t));
      printf("%d\n%d\ n", t.a, t.c);
      printf(" %d \n",t.T->ucType);
      return 0;


      }
      the structure defination is same as above. By the by I am using gcc
      compiler.

      Comment

      • Steven Kirby

        #4
        Re: structure pointers

        raghu wrote:
        Richard Heathfield wrote:
        >raghu said:
        >>
        >>Hello
        >>>
        >>This is Raghu. I have a problem in the pointers.
        >>The code is
        >>typedef struct Tlv{
        >> /*type Length value for each
        >>message */
        >> unsigned char ucType; /*Type varies from message to
        >>message */
        >> unsigned short usLength;/*Length */
        >> unsigned int ucValue;/*Value*/
        >>}tlv;
        >>>
        >>>
        >>typedef struct te{
        >>char d;
        >>int e;
        >>}Q;
        >>struct temp{
        >>int a;
        >>unsigned int c;
        >>Q *q;
        >>tlv *T;
        >>};
        >> struct temp t;
        >> t.a = 4;
        >Here's your problem. You can't have executable code except inside a
        >function.
        >>
        >> t.c = 31;
        >This breaks for the same reason.
        >>
        >> t.q[0].d =21;
        >This breaks for the same reason, but also because t.q doesn't point anywhere
        >useful (or if it was statically initialised, it has the value NULL). Either
        >way, t.q[0] is an illegal memory access.
        >>
        >--
        >Richard Heathfield
        >"Usenet is a strange place" - dmr 29/7/1999
        >http://www.cpax.org.uk
        >email: rjh at above domain (but drop the www, obviously)
        >
        But even I modified the code as follows the error is repeting.
        the code is
        main(){
        struct temp t;
        t.a = 4;
        t.c = 31;
        >
        >
        t.T -ucType = 1;
        t.T -usLength = 11;
        t.T -ucValue = 21;
        printf("The entered values are %d\n ", sizeof(t));
        printf("%d\n%d\ n", t.a, t.c);
        printf(" %d \n",t.T->ucType);
        return 0;
        >
        >
        }
        the structure defination is same as above. By the by I am using gcc
        compiler.
        >
        In the temp struct definition, you define T as a pointer to a tlv
        structure, but you never actually assign it to one. You're trying to use
        an undefined pointer, which is most likely why it's crashing. Either you
        need to assign the T pointer to a tlv struct, or just declare T as a tlv
        struct variable, not a pointer.

        Comment

        • raghu

          #5
          Re: structure pointers


          Steven Kirby wrote:
          raghu wrote:
          Richard Heathfield wrote:
          raghu said:
          >
          >Hello
          >>
          >This is Raghu. I have a problem in the pointers.
          >The code is
          >typedef struct Tlv{
          > /*type Length value for each
          >message */
          > unsigned char ucType; /*Type varies from message to
          >message */
          > unsigned short usLength;/*Length */
          > unsigned int ucValue;/*Value*/
          >}tlv;
          >>
          >>
          >typedef struct te{
          >char d;
          >int e;
          >}Q;
          >struct temp{
          >int a;
          >unsigned int c;
          >Q *q;
          >tlv *T;
          >};
          > struct temp t;
          > t.a = 4;
          Here's your problem. You can't have executable code except inside a
          function.
          >
          > t.c = 31;
          This breaks for the same reason.
          >
          > t.q[0].d =21;
          This breaks for the same reason, but also because t.q doesn't point anywhere
          useful (or if it was statically initialised, it has the value NULL). Either
          way, t.q[0] is an illegal memory access.
          >
          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at above domain (but drop the www, obviously)
          But even I modified the code as follows the error is repeting.
          the code is
          main(){
          struct temp t;
          t.a = 4;
          t.c = 31;


          t.T -ucType = 1;
          t.T -usLength = 11;
          t.T -ucValue = 21;
          printf("The entered values are %d\n ", sizeof(t));
          printf("%d\n%d\ n", t.a, t.c);
          printf(" %d \n",t.T->ucType);
          return 0;


          }
          the structure defination is same as above. By the by I am using gcc
          compiler.
          >
          In the temp struct definition, you define T as a pointer to a tlv
          structure, but you never actually assign it to one. You're trying to use
          an undefined pointer, which is most likely why it's crashing. Either you
          need to assign the T pointer to a tlv struct, or just declare T as a tlv
          struct variable, not a pointer.
          Hello,

          Thank Q very much. Your idea worked. I want to know which C
          compiler is the best one.
          please inform me.
          once again Thank Q
          Bye
          Raghu

          Comment

          • Richard Heathfield

            #6
            Re: structure pointers

            raghu said:
            >
            I want to know which C
            compiler is the best one.
            Define "best".

            --
            Richard Heathfield
            "Usenet is a strange place" - dmr 29/7/1999

            email: rjh at above domain (but drop the www, obviously)

            Comment

            • Steven Kirby

              #7
              Re: structure pointers

              raghu wrote:
              Steven Kirby wrote:
              >raghu wrote:
              >>Richard Heathfield wrote:
              >>>raghu said:
              >>>>
              >>>>Hello
              >>>>>
              >>>>This is Raghu. I have a problem in the pointers.
              >>>>The code is
              >>>>typedef struct Tlv{
              >>>> /*type Length value for each
              >>>>message */
              >>>> unsigned char ucType; /*Type varies from message to
              >>>>message */
              >>>> unsigned short usLength;/*Length */
              >>>> unsigned int ucValue;/*Value*/
              >>>>}tlv;
              >>>>>
              >>>>>
              >>>>typedef struct te{
              >>>>char d;
              >>>>int e;
              >>>>}Q;
              >>>>struct temp{
              >>>>int a;
              >>>>unsigned int c;
              >>>>Q *q;
              >>>>tlv *T;
              >>>>};
              >>>> struct temp t;
              >>>> t.a = 4;
              >>>Here's your problem. You can't have executable code except inside a
              >>>function.
              >>>>
              >>>> t.c = 31;
              >>>This breaks for the same reason.
              >>>>
              >>>> t.q[0].d =21;
              >>>This breaks for the same reason, but also because t.q doesn't point anywhere
              >>>useful (or if it was statically initialised, it has the value NULL). Either
              >>>way, t.q[0] is an illegal memory access.
              >>>>
              >>>--
              >>>Richard Heathfield
              >>>"Usenet is a strange place" - dmr 29/7/1999
              >>>http://www.cpax.org.uk
              >>>email: rjh at above domain (but drop the www, obviously)
              >>But even I modified the code as follows the error is repeting.
              >>the code is
              >>main(){
              >> struct temp t;
              >> t.a = 4;
              >> t.c = 31;
              >>>
              >>>
              >> t.T -ucType = 1;
              >> t.T -usLength = 11;
              >> t.T -ucValue = 21;
              >> printf("The entered values are %d\n ", sizeof(t));
              >> printf("%d\n%d\ n", t.a, t.c);
              >> printf(" %d \n",t.T->ucType);
              >> return 0;
              >>>
              >>>
              >>}
              >> the structure defination is same as above. By the by I am using gcc
              >>compiler.
              >>>
              >In the temp struct definition, you define T as a pointer to a tlv
              >structure, but you never actually assign it to one. You're trying to use
              >an undefined pointer, which is most likely why it's crashing. Either you
              >need to assign the T pointer to a tlv struct, or just declare T as a tlv
              >struct variable, not a pointer.
              >
              Hello,
              >
              Thank Q very much. Your idea worked. I want to know which C
              compiler is the best one.
              please inform me.
              once again Thank Q
              Bye
              Raghu
              >
              That's pretty much a matter of opinion. Personally, I use MinGW (a
              Windows port of the GCC compiler, and some other tools) for console and
              SDL programming, but I find Visual C++ to be much handier for Windows
              programming.

              I'm not sure what you're using at the moment, but Dev-C++ is a simple
              and practical environment for Windows which comes bundled with MinGW.
              You can get it from http://www.bloodshed.net/devcpp.html

              Comment

              • Fred Kleinschmidt

                #8
                Re: structure pointers


                "raghu" <ragavakumar@gm ail.comwrote in message
                news:1162192614 .605043.263510@ m7g2000cwm.goog legroups.com...
                Hello
                >
                This is Raghu. I have a problem in the pointers.
                The code is
                typedef struct Tlv{
                /*type Length value for each
                message */
                unsigned char ucType; /*Type varies from message to
                message */
                unsigned short usLength;/*Length */
                unsigned int ucValue;/*Value*/
                }tlv;
                >
                >
                typedef struct te{
                char d;
                int e;
                }Q;
                struct temp{
                int a;
                unsigned int c;
                Q *q;
                tlv *T;
                };
                struct temp t;
                t.a = 4;
                t.c = 31;
                t.q[0].d =21;
                t.q[0].e = 23;
                t.q[1].d = 11;
                t.q[1].e = 4;
                >
                Another problem here. What is t.q?
                You have not assigned q to point anywhere
                >
                t.T -ucType = 1;
                t.T -usLength = 11;
                t.T -ucValue = 21;
                printf("The entered values are ");
                printf("%d\n%d\ n", t.a, t.c);
                printf(" %d \n", t.q[0].d);
                printf(" %d \n",t.T->ucType);
                return 0;
                }
                when I run this program some error is generating that is Segmentation
                fault.
                Can you please help me.
                Thanks in advance.
                >
                -Bye
                Raghu
                >
                --
                Fred L. Kleinschmidt
                Boeing Associate Technical Fellow
                Technical Architect, Software Reuse Project


                Comment

                Working...