String Manipulation error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • selvesteen@hotmail.com

    #1

    String Manipulation error

    Hello All,

    I am getting following error on HP-UX for the program given below

    "Pid 10150 in trap loop, signal 11
    Memory fault"

    int main()
    {
    char *txtptr="";
    const char *a = "string append";
    strcat(txtptr,a );
    printf("%s\n",t xtptr);
    }
    What is wrong with this program? What is a better method to initialize
    a string (char *txtptr="") with NULL.

    Advance thanks for the help.
    Mike

  • David Resnick

    #2
    Re: String Manipulation error

    selvesteen@hotm ail.com wrote:[color=blue]
    > Hello All,
    >
    > I am getting following error on HP-UX for the program given below
    >
    > "Pid 10150 in trap loop, signal 11
    > Memory fault"
    >
    > int main()
    > {
    > char *txtptr="";
    > const char *a = "string append";
    > strcat(txtptr,a );
    > printf("%s\n",t xtptr);
    > }
    > What is wrong with this program? What is a better method to initialize
    > a string (char *txtptr="") with NULL.
    >
    > Advance thanks for the help.
    > Mike[/color]

    Try the FAQ:



    You need to read up about memory allocation...

    -David

    Comment

    • Yohji

      #3
      Re: String Manipulation error



      selvesteen@hotm ail.com wrote:[color=blue]
      > Hello All,
      >
      > I am getting following error on HP-UX for the program given below
      >
      > "Pid 10150 in trap loop, signal 11
      > Memory fault"
      >
      > int main()
      > {
      > char *txtptr="";
      > const char *a = "string append";
      > strcat(txtptr,a );
      > printf("%s\n",t xtptr);
      > }
      > What is wrong with this program? What is a better method to initialize
      > a string (char *txtptr="") with NULL.
      >
      > Advance thanks for the help.
      > Mike[/color]

      Yeah,this is one of the common mistakes.That is just because you did
      not alloc enough mem for 'txtptr'.You should know that the function
      strcat() does NOT alloc mem for you.So you can try this:

      #define MAX 255
      ....
      char txtptr[MAX]="";

      Comment

      • Eric Sosman

        #4
        Re: String Manipulation error

        selvesteen@hotm ail.com wrote:
        [color=blue]
        > Hello All,
        >
        > I am getting following error on HP-UX for the program given below
        >
        > "Pid 10150 in trap loop, signal 11
        > Memory fault"
        >
        > int main()
        > {
        > char *txtptr="";
        > const char *a = "string append";
        > strcat(txtptr,a );
        > printf("%s\n",t xtptr);
        > }
        > What is wrong with this program? What is a better method to initialize
        > a string (char *txtptr="") with NULL.[/color]

        It may not look like it at first glance, but this is
        Question 7.1 in the comp.lang.c Frequently Asked Questions
        (FAQ) list



        --
        Eric Sosman
        esosman@acm-dot-org.invalid

        Comment

        • Alexei A. Frounze

          #5
          Re: String Manipulation error

          <selvesteen@hot mail.com> wrote in message
          news:1121341607 .637619.126580@ z14g2000cwz.goo glegroups.com.. .[color=blue]
          > Hello All,
          >
          > I am getting following error on HP-UX for the program given below
          >
          > "Pid 10150 in trap loop, signal 11
          > Memory fault"
          >
          > int main()
          > {
          > char *txtptr="";
          > const char *a = "string append";
          > strcat(txtptr,a );
          > printf("%s\n",t xtptr);
          > }
          > What is wrong with this program? What is a better method to initialize
          > a string (char *txtptr="") with NULL.
          >
          > Advance thanks for the help.
          > Mike[/color]

          Did you learn basic or pascal before? That would explain it :)

          Alex


          Comment

          • Martin Ambuhl

            #6
            Re: String Manipulation error

            selvesteen@hotm ail.com wrote:
            [color=blue]
            > int main()
            > {
            > char *txtptr="";
            > const char *a = "string append";
            > strcat(txtptr,a );
            > printf("%s\n",t xtptr);
            > }
            > What is wrong with this program?[/color]

            The usual things, asked about every day by people who neither read the
            FAQ nor follow the newsgroups.
            1) The big one: txtptr is points to a string literal which is one char
            wide; that string literal neither is guaranteed to be writable nor has
            any additional space assigned to it.
            2) Some others: failure to #include <string.h>, failure to #include
            <stdio.h>, failure to return a value from main().
            [color=blue]
            > What is a better method to initialize
            > a string (char *txtptr="") with NULL.[/color]

            You don't want to initialize the string with NULL, which is the name of
            a null-pointer constant.

            #define BIGENOUGH 4096 /* or whatever is big enough */
            [...]
            char txt[BIGENOUGH] = "";

            Comment

            Working...