Page 1 of 2
composing a string
Posted: Sat Sep 29, 2007 12:07 pm
by mohammed
hi all i am trying to make a string so i can print it with the function puts(unsigned char *str)
that's my function
Code: Select all
gets(unsigned char *string)
{
char c;
int cntr = 0;
while(c=getch() != '\n')
{
string[cntr] = c;
cntr++
}
cntr++;
string[cntr] = '\0';
}
is there something wrong with this function ?????
Posted: Sat Sep 29, 2007 12:36 pm
by bluecode
First of all the function prototype is not C99 compliant. It should be:
Second, do you know that getch() belongs to curses. In order to use that function you should initialize curses. See initscr or newterm for more information.
Third, the cntr++ after the while loop should not be there.
Fourth, why do you return the first character?
Re: composing a string
Posted: Sat Sep 29, 2007 1:01 pm
by Solar
mohammed wrote:is there something wrong with this function ?????
Code: Select all
$ gcc -Wall -Wextra -c test.c
test.c: 2: warning: return type defaults to 'int'
test.c: 5: warning: implicit declaration of function 'getch'
test.c: 5: warning: suggest parentheses around assignment used as truth value
test.c: 9: error: expected ';' before '}' token
Both c and cntr are completely redundant. I could show you how, but I rather suggest you learn about pointers before continuing.
Posted: Sat Sep 29, 2007 8:06 pm
by mohammed
first okay i changed the prototype scond getc() is my function that returns a char and working well i wrote it getch as i used to write in in C i am sorry
third where should it be ??
fourth i thought that i am just returning the pointer of the beginning of the string ...
Code: Select all
Both c and cntr are completely redundant. I could show you how, but I rather suggest you learn about pointers before continuing.
the pointer points to the place of the variable instead of dealing with the value you deal with the address of the value here our pointer points to a C string that should end with the terminate zero '\0'
i assure you that i know pointers show me how then i think you punished me enough in the last topic
Posted: Sat Sep 29, 2007 8:54 pm
by Alboin
mohammed wrote:i assure you that i know pointers show me how then i think you punished me enough in the last topic
A pointer's address may be increased, like an integer's. For example:
Increases the pointer forward to it's next address. (ie. based on it's type.) Moreover, the current value of the pointer may be set like:
I believe it was the combination of these two things that Solar was referring to. I won't show you, but by using the above, you can remove the two aforementioned variables from your code.
Yea .. there are some errors
Posted: Sun Sep 30, 2007 12:15 am
by DeletedAccount
First of all there is a problem of buffer overlow and using gets is
not recommended .. Your implementation of gets doesnt seem to fix
the problem .In your programs if the user enters a very loooong
string ... The program will crash with Segmentation Fault core dumped...
so use 1) fgets -- use STDIN instead as the filename
or open () in a similar fashion ...
Posted: Sun Sep 30, 2007 2:27 am
by mohammed
Code: Select all
char *gets(char *string)
{
char c;
while(c=getc() != '\n')
{
*string++ = c;
}
*string++ = '\0';
}
then this function have no problems at all ??? it suppose to work fine ??
is it a policy here not to showany one a code ??? or what ???
i tried this in main function
Code: Select all
char *zeko;
char *medo = "fdsfs";
char m;
m=getc();
putc(m); //worked
puts(medo); worked
gets(zeko);
puts(zeko); // didn't work : ( (
Posted: Sun Sep 30, 2007 3:17 am
by bluecode
mohammed wrote:is it a policy here not to showany one a code ??? or what ???
No, but we have the interest that you learn as much as possible from our answers. And for me (others see this different, e.g. Dex) learning has nothing to do with copy & pasting sourcecode.
About your problem: Pointers should point to something, right? But where does zeko point to? Read something about memory (de)allocation, the heap (or free store) and the like. Functions in the C library for that purpose are: malloc, calloc, realloc & free
Posted: Sun Sep 30, 2007 8:32 pm
by mohammed
then the problem is with my pointer that i am trying to pass to gets not with gets itself ???
then an allocate function will solve the problem i mean you tried that before
and it will never work without malloc function ??
Posted: Mon Oct 01, 2007 12:20 am
by bluecode
mohammed wrote:and it will never work without malloc function ??
You can define a local/global array of chars and pass a pointer to the first element of that array to your gets() function.
Posted: Mon Oct 01, 2007 12:29 am
by mohammed
that tried the arrays before and it worked but that is not what i want i want to compose a string that all C functions can deal with in normal C you just type char *str;
and then
gets(str);
gets return the string that you typed in str without any allocation of any kind ...!!
Posted: Mon Oct 01, 2007 12:58 am
by pcmattman
I've never seen
You
must allocate space for str, otherwise it isn't pointing anywhere, and gets is writing to what is possibly not even physical memory.
Posted: Mon Oct 01, 2007 3:43 am
by JamesM
mohammed wrote:Code: Select all
char *gets(char *string)
{
char c;
while(c=getc() != '\n')
{
*string++ = c;
}
*string++ = '\0';
}
then this function have no problems at all ??? it suppose to work fine ??
is it a policy here not to showany one a code ??? or what ???
i tried this in main function
Code: Select all
char *zeko;
char *medo = "fdsfs";
char m;
m=getc();
putc(m); //worked
puts(medo); worked
gets(zeko);
puts(zeko); // didn't work : ( (
1) 'zeko' is undefined. You are writing to duff memory somewhere. (you're bloody lucky it isn't segfaulting). By the way, is this in a linux environment or in your own OS? (I ask because of all the time you spent getting a 'getc' function to work...)
2) As mentioned, your function is not overflow-safe. The function does not know anything about the maximum size of the input buffer, and as such can overflow. A safer prototype would be:
Code: Select all
char *gets(char *buffer, unsigned int max_length)
3) Your function prototype says it returns a "char *", yet there is no return statement.
4) the '++' part on '*string++ = '\0'' is redundant, you are not using string any more so there is no need for the extra increment.
5) personally I would parathesise the "c=getc()" expression to ease readablilty. It is a moot point, as because of operator prescdence it will work anyway, but I tend not to trust operator prescendence too much.
6) Please learn more about pointers.
mohammed wrote:that tried the arrays before and it worked but that is not what i want i want to compose a string that all C functions can deal with in normal C you just type char *str;
and then
gets(str);
gets return the string that you typed in str without any allocation of any kind ...!!
Completely and utterly incorrect. Allocation must be made somewhere, be it in the calling function or the callee. It is safer to allocate in the calling function as, in such cases, stack allocation can be used. However, if you want to do callee allocation:
Code: Select all
#define BUFSZ 512
char *gets()
{
char *buffer = (char*)malloc(BUFSZ);
unsigned int sz = 0;
while( (*buffer++=getc()) != '\n' &&
sz < BUFSZ-1)
sz++;
*--buffer = '\0';
return buffer;
}
... in main.c ...
char *string = gets();
free(string);
but note the static allocation size! this is not the best way to do this! (also note that I fixed your code
)
JamesM
Posted: Mon Oct 01, 2007 3:48 am
by Combuster
Actually, I'd consider it unlucky that it doesn't segfault...
Posted: Mon Oct 01, 2007 4:48 am
by mohammed
sorry it should be char zeko[20];
working in bran's kernel so there will never be any segfault cause this pointer will be in the uninitialized data in .bss section ....(i think the linker script control both the .c files and asm files as well )right ?
but note the static allocation size! this is not the best way to do this! (also note that I fixed your code Wink )
JamesM
you are my best friend right here JamesM > : D < even my getc() function based on what you instructed me ...i mean about testing the 0x64 register
i don't have any malloc or free functions yet i am gonna writ....aaa... copy it to try your code and i will tell you okay ?