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';
}
Code: Select all
gets(unsigned char *string)
{
char c;
int cntr = 0;
while(c=getch() != '\n')
{
string[cntr] = c;
cntr++
}
cntr++;
string[cntr] = '\0';
}
Code: Select all
char *gets(char *s);
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
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.
A pointer's address may be increased, like an integer's. For example:mohammed wrote:i assure you that i know pointers show me how then i think you punished me enough in the last topic
Code: Select all
pointer++;
Code: Select all
*pointer = value;
Code: Select all
char *gets(char *string)
{
char c;
while(c=getc() != '\n')
{
*string++ = c;
}
*string++ = '\0';
}
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 : ( (
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.mohammed wrote:is it a policy here not to showany one a code ??? or what ???
Code: Select all
char* str;
gets( str );
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...)mohammed wrote:then this function have no problems at all ??? it suppose to work fine ??Code: Select all
char *gets(char *string) { char c; while(c=getc() != '\n') { *string++ = c; } *string++ = '\0'; }
is it a policy here not to showany one a code ??? or what ???
i tried this in main functionCode: Select all
char *zeko; char *medo = "fdsfs"; char m; m=getc(); putc(m); //worked puts(medo); worked gets(zeko); puts(zeko); // didn't work : ( (
Code: Select all
char *gets(char *buffer, unsigned int max_length)
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: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 ...!!
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);
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 registerbut note the static allocation size! this is not the best way to do this! (also note that I fixed your code Wink )
JamesM