Page 1 of 1

how to go to the beginning of a pointer?

Posted: Wed Oct 23, 2002 3:11 pm
by gtsphere
is there any way to complete destroy a string so i can reuse it? IE: my CLI, i type, it saves everything into a string untill i hit enter, then it null terminates it, and then i try to rewrite over it, but i can't get back to the begninning of it? how would i do that?

*string[0] ?

thanks in advance

Re:how to go to the beginning of a pointer?

Posted: Wed Oct 23, 2002 3:21 pm
by dronkit

Code: Select all

char buffer[4096];
char *pbuffer = buffer;
So you keep the start of your buffer and use a temporal variable to traverse through it. you should *never* loose this kind of pointers, otherwise you will be doomed to memory leaks and unstable code ;)

if you malloc() memory, save this pointer so you can free() it later.

Re:how to go to the beginning of a pointer?

Posted: Wed Oct 23, 2002 3:30 pm
by gtsphere
char input[MAXBUFLEN];   //the users input, MAX
char *pointerinput = input;   //pointer to input

those are the variables, and below is the code i use when the user hits ENTER
         
   
         
            *pointerinput++ = '\0';   //null terminate our pointer!

            printf("\nUnknown Command %s ", input );   //give simple error for now!

            y++;

            int i = 0;
            int length = 0;
            length = strlen( pointerinput );

            for( ; i<=length; i++ )
            {
               pointerinput = ' ';
            }

            gotoxy( 0, y );   //start cursor there!


what'ca think?

Re:how to go to the beginning of a pointer?

Posted: Wed Oct 23, 2002 3:34 pm
by dronkit
you can optimize it like this:

Code: Select all


int
getuserinput(char *buffer, ssize_t maxlen)
{
   // read from keyboard
   if(strlen(new_input) >= maxlen)
      return -1;
   // go on with the command.  copy command.
   memcpy((void *)buffer, (void *)new_input, strlen(new_input));
   return 0;
}

cli(void)
{
   char buffer[4096];

   while(1)
   {
      memset((void *)buffer, 0, sizeof(buffer));    // zero entire buffer
      getuserinput(buffer, sizeof(buffer));            // save the user input
   }
}