how to go to the beginning of a pointer?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
gtsphere

how to go to the beginning of a pointer?

Post 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
dronkit

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

Post 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.
gtsphere

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

Post 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?
dronkit

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

Post 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
   }
}
Post Reply