Menu

segmentation fault error

M. Vora
2003-07-31
2012-09-26
  • M. Vora

    M. Vora - 2003-07-31

    why I am getting segmentation fault error during the second cycle and not during the first.

    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>

    int main()
    {
      struct str_misc {
        char* cstr;
      };
      str_misc str_ex[12];
      char buf[12];

      for (int i = 0; i <12; i++) {
        cin>>buf;
        strcpy(str_ex[i].cstr,buf) ;
      }

          system("PAUSE");
          return 0;
    }

    if i run this code, when i=0, everything is fine.  But when it enters the loop second time i.e when i = 1, i get a segmentation fault error.  I am not able to understand this behaviour.  When I change the definition of cstr to an array, everything is fine.  but when cstr is a pointer to char, as in the above case, then I get the problem.

     
    • Wayne Keen

      Wayne Keen - 2003-07-31

      For a programming question, it is probably better to post to the other forum (Bloodshed Forum) rather than the developers forum....

      Wayne

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.