ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.