code optimization

This is a discussion on code optimization within the C Programming forums, part of the General Programming Boards category; I was reading through my programming book when I came upon two programs-one is a revisement of the other. The ...

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    Question code optimization

    I was reading through my programming book when I came upon two programs-one is a revisement of the other. The programs get your name, then print it on the screen, then print it backwards using pointers. The first version uses a for loop to get input

    Code:
    for(i=o; i<MAXSTRING; i++)
    {
      buff[i]=getch();
      printf("%c", buff[i]);
      if(buff[i]==ENTER)
        {
          buff[i]= '\0';  /*puts null termination at end of string*/
          break;
        }
    }
    the second version uses a pointer

    Code:
    int main()
    {
     char c, buff[MAXSTRING], *cptr;
     int length;
    
     cptr=buff;
     printf("Enter your name: ");
     while( (c=getch()) != ENTER)
       {
         *cptr=c;
         printf("%c",*cptr);
         cptr++;
       }
     *cptr='\0';
    .......
    }
    I noticed that when the pointer is used it allows you to input past MAXSTRING (I used 15). I was wondering if there was a way to check to ensure that input doesn't overflow only using *cptr so that another variable doesn't have to be used. I tried adding an if statement inside the while loop to check if the value of buff[0] plus MAXSTRING would equal the value of *cptr (plus 15 bytes because it is only a char), and then if it was equal it would exit the loop. I tried this and it doesn't work

    Code:
    while( (c=getch()) != ENTER)
       {
         if(cptr==(&buff[0]+MAXSTRING)
           { break; }
         else  
          {
           *cptr=c;
           printf("%c",*cptr);
           cptr++;
           }
         }
    Can this be done, and if so how?

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Thanks for the help.
    Re: Salem
    >> I tried this and it doesn't work
    >Can you be more specific?

    Did you notice my third section of code that showed what I'd tried?

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Between my sentece or two explaining what I had tried to do and the actual code I used when I changed the program I don't see how I could have given much more information. Past what I posted to you I didn't know what was going on or what part of my editing didn't work.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void rev( void )
    {
        int c;
        if( (c = getchar()) != '\n' )
            rev( );
        putchar( c );
    }
    Wheee...

    Code:
    char buf[BUFSIZ];
    char *ptr;
    
    if( fgets( buf, BUFSIZ, stdin ) )
    {
        for( ptr = buf+strlen(buf)-1; ptr>= buf; ptr-- )
            putchar( *ptr );
    }
    Ooooooh. Aaaaah.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. code optimization
    By gagig in forum C Programming
    Replies: 6
    Last Post: 02-21-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21