LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2005, 09:41 PM   #16
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53

Quote:
also, when i convert the string to integers, it converts it into the integer value of the ascii code, not the equivalent integer. so '0' becomes 48, '1' becomes 49, etc etc. so right now i subtract 48, and '0' = 0, '1'=1, and so on, but this seems like a bit of an ugly hack to me. is there another preferred way of doing this?
Actualy more or less thats what atoi does:
when you have a string "123" or better '1' '2' '3' '\0' what atoi will do is ((('1'-48)*10 + ('2'-48))*10+'3'-48)*10=100 +20 +3=123 using a for loop

Last edited by perfect_circle; 02-26-2005 at 07:30 AM.
 
Old 02-26-2005, 03:12 PM   #17
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I know I'm coming into this discussion late, but here's a slightly modified version of your code mcd.

Code:
char *getstring ( void )
{
  char *q;  /* There is no need to pass in q if you disregard its value :) */
  int size_allocated = 5*sizeof( char );   /* initially allocate this amount */

  q = malloc( size_allocated );
  if (q==0)                                /* I got this from RUTE Users Tutorial =) */
  {
    perror("malloc failed!\n);
    abort();
  }
  memset( q, 0, size_allocated );

  printf("Please input a number\n");

  int c=0;
  while (q[c-1] != '\n')                   /* if the users types a \n, the next time this is processed it will exit */
  {
    if (c == ( size_allocated - 2 ))       /* size_allocated is the size of your buffer.
                                              reallocate when the next character would overwrite the final NULL */
    {
      q = realloc(q, size_allocated * 2);
      if (q==0)
      {
        perror("realloc failed!\n);
        abort();
      }
      memset( q + size_allocated, 0, size_allocated );
      size_allocated = size_allocated*2;
    }
    q[c]=getchar();                        /* after all that, this is what's being done, lol */
    c++;
  }

  return q;
}
That's how I would approach it if I were to follow what you're shooting for. Obviously, the stuff in blue is modified/new code. I use memset() to guarantee your string has (at least one) final NULL character. The memset after reallocation should leave the previous string unaltered, but initialize the new portion.

While this method will allow dynamic changing of the buffer size, it's a little difficult to handle mistakes. Say the user types in a letter by accident, or hits the backspace/delete key.

EDIT:
I accidentally had the condition listed incorrectly. It was "c < ( size_allocated - 2 )", and it should be what I have written now: "c == ( size_allocated - 2 )"

Last edited by Dark_Helmet; 02-26-2005 at 04:51 PM.
 
Old 02-26-2005, 04:01 PM   #18
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
int c=0;
while (q[c-1] != '\n') /* if the users types a \n, the next time this is processed it will exit */
I don't like this one.
q[-1] is out of the range of the string
 
Old 02-26-2005, 04:56 PM   #19
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
That's a good point. A do-while loop would probably be better:
Code:
int c=0;
do
{

   ...

  c++;
} while ( q[c-1] != '\n' );
 
Old 02-26-2005, 08:04 PM   #20
mcd
Member
 
Registered: Aug 2003
Location: Denver, CO
Distribution: CentOS, Debian
Posts: 825

Original Poster
Rep: Reputation: 33
great, thanks for the comments guys! i'm making the changes now.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strlen() function problem in C++ sajith Programming 9 01-16-2006 09:14 PM
strlen problem salahuddin_66 Programming 2 11-13-2005 04:21 AM
C++ question: strlen error!? Hady Programming 2 03-16-2005 05:08 AM
I am confused... odious1 Linux - Networking 3 11-01-2003 03:37 AM
so confused... KeTrueno Linux - General 1 09-28-2003 01:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration