Thread: variables

  1. #1
    Registered User Unimatrix139's Avatar
    Join Date
    Jun 2002
    Posts
    55

    Angry variables

    If I define a variable in a procedure, can I call the procedure again and again without problems? I am using code like this:-

    Code:
    void func02()
    {
       char buffer[700]
       strcpy(buffer, "Test");
       printf("The value of buffer is %s\n", buffer);
       nextfunc();
    }
    When I run my application, a stack overflow always occours after accessing this procedure about 7 times. Is the variable (buffer) causing the problems? Thanks
    Kree'ta Tau'ri! Chaapa'ai!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >a stack overflow always occours
    It may be a contributing factor, what does nextfunc() do?

    >char buffer[700]
    >strcpy(buffer, "Test");
    This looks like a test program, but this is a terrible waste of space.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User Unimatrix139's Avatar
    Join Date
    Jun 2002
    Posts
    55

    funcnext

    Its just an example of what me program does. The real thing goes like this:-

    Code:
    prompt()
    {
       char buffer[1000];
       int i;
       if(promptr!=NULL)
       {
          printf("%s ", promptr);
       }
       inputstr=gets(sizedstring1);
       if(strlen(inputstr)>692)
       {
          error3(41, NULL);
          prompt();
       }
       starstring=inputstr;
       if(strcmp(starstring, "\n")!=0) { fprintf(perm, "%s\n", starstring); }
       if(privstat!=0)
       {
          i=0;
          do
          {
             buffer[i]=NULL;
             i=i+1;
          }
          while(i<sizeof(buffer));
          strcpy(buffer, "p=");
          strcat(buffer, starstring);
          starstring=eciread("[ACCESS]", buffer, 700, 0, 1);
          if(starstring!=NULL)
          {
             error3(40, NULL);
             prompt();
          }
       }
       process(inputstr, 1, 1);
    }
    void process(char *processr, int a, int b)
    {
       if(b==1) { refresh(0); }
       if(k==1)
       {
          stdint1=0;
          fp=fopen("~tmp01.tmo", "a");
          while(stdint1<2000)
          {
             peek(0, stdint1, starstring, 54);
             fprintf(fp, starstring);
             stdint1=stdint1+1;
          }
          starstring=NULL;
          stdint1=0;
          fclose(fp);
       }
       if(strncmp(processr, "", 1)==0)
       {
          if(a!=2) { prompt(); }
       }
       /* more strncmp statements all branching off to different procedures */
       prompt();
    }
    Kree'ta Tau'ri! Chaapa'ai!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Rewrite it using a while loop in prompt(), so that if the user gets it wrong, or wants another go, then you just repeat the while loop

    Ditto with process(), this should just return to prompt() for another iteration of the process, not call prompt() recursively

  5. #5
    Registered User Unimatrix139's Avatar
    Join Date
    Jun 2002
    Posts
    55

    :D gd idea

    Good idea - i'll re-write both of them with loops instead. Thanks Salem and Prelude
    Kree'ta Tau'ri! Chaapa'ai!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM