使用 getchar 函数创建一个 gets 函数,该函数 return 在 return 上按下一个 char*
Creating a gets function that returns a char* on return pressed using a getchar function
这个问题我有一段时间了。我无法创建允许用户使用 get_char() 函数获取 char* 的 gets(int maxChar) 函数。
我当前的代码:
char* gets(int maxChar) {
char a;
char* b;
int i;
for(i = 0; i<maxChar; i = i + 1){
a = getchar();
if (a != 0x0D) {putchar(a);} else {puts("\r\n");break;}
b[sizeof(b)] = a;
}
if (sizeof(b) > maxChar-1) {puts("\r\n");}
return b;
//if (b[sizeof(b)-1] != '')
}
get_char() 函数完美运行。
完整 kernel.c: https://pastebin.com/3bqTbHsv
改变
char* b;
至
char* b = (char *)malloc(maxChar);
和
b[sizeof(b)] = a;
至
b[i] = a;
另外,改变
if (sizeof(b) > maxChar-1) {puts("\r\n");}
至
if (i > maxChar-1) {puts("\r\n");}
所做的更改是:
您创建了一个指针,但还没有分配任何内存。因此 malloc
语句。
对于 32 位编译器,sizeof(b)
将始终是 4
。您需要数组索引,由 i
.
给出
同2.
这些是您需要进行的基本更改,无需对您的逻辑进行任何更改。
以下是对您尝试执行的操作的有根据的猜测。它包括一些解释和一个调用示例:
char* _gets(int maxChar) // change name to avoid conflict with library 'gets'
{
//char a; //addresses concern pointed out in comments.
int a; //variable is used by function that can return EOF (-1)
int i=0; //
char* b = calloc(maxChar + 1, 1);//point pointer to memory (that it owns)
if(b) //test for success before going on
{
for(i = 0; i<maxChar-3; i = i + 1) // -3 to leave room for \r\n[=10=]
{
a = getchar();
if ((a != 0x0D) && (a != EOF))
{
putchar(a);
b[i]=a;// place char into string accumulator
}
else break;// either EOF or <ENTER> occurred
} // no need for else in this implementation as we can handle
// the clean up once the conditions are met by outputting
// user's entries, then preparing your string (array) for return
puts("\r\n");
b[i++]='\r';
b[i++]='\n';
b[i++]='[=10=]';
}
else return NULL;//failed to allocate memory. leave
return b; // return b (now a string)
//if (b[sizeof(b)-1] != '') // not sure what this was for
}
int main(void)
{
char *strArray = _gets(10);//creates memory for strArray
if(strArray) //use only if not NULL
{
printf("The array contains: %s", strArray );
free(strArray );
}
return 0;
}
这个问题我有一段时间了。我无法创建允许用户使用 get_char() 函数获取 char* 的 gets(int maxChar) 函数。
我当前的代码:
char* gets(int maxChar) {
char a;
char* b;
int i;
for(i = 0; i<maxChar; i = i + 1){
a = getchar();
if (a != 0x0D) {putchar(a);} else {puts("\r\n");break;}
b[sizeof(b)] = a;
}
if (sizeof(b) > maxChar-1) {puts("\r\n");}
return b;
//if (b[sizeof(b)-1] != '')
}
get_char() 函数完美运行。
完整 kernel.c: https://pastebin.com/3bqTbHsv
改变
char* b;
至
char* b = (char *)malloc(maxChar);
和
b[sizeof(b)] = a;
至
b[i] = a;
另外,改变
if (sizeof(b) > maxChar-1) {puts("\r\n");}
至
if (i > maxChar-1) {puts("\r\n");}
所做的更改是:
您创建了一个指针,但还没有分配任何内存。因此
malloc
语句。
对于 32 位编译器,sizeof(b)
将始终是4
。您需要数组索引,由i
. 给出
同2.
这些是您需要进行的基本更改,无需对您的逻辑进行任何更改。
以下是对您尝试执行的操作的有根据的猜测。它包括一些解释和一个调用示例:
char* _gets(int maxChar) // change name to avoid conflict with library 'gets'
{
//char a; //addresses concern pointed out in comments.
int a; //variable is used by function that can return EOF (-1)
int i=0; //
char* b = calloc(maxChar + 1, 1);//point pointer to memory (that it owns)
if(b) //test for success before going on
{
for(i = 0; i<maxChar-3; i = i + 1) // -3 to leave room for \r\n[=10=]
{
a = getchar();
if ((a != 0x0D) && (a != EOF))
{
putchar(a);
b[i]=a;// place char into string accumulator
}
else break;// either EOF or <ENTER> occurred
} // no need for else in this implementation as we can handle
// the clean up once the conditions are met by outputting
// user's entries, then preparing your string (array) for return
puts("\r\n");
b[i++]='\r';
b[i++]='\n';
b[i++]='[=10=]';
}
else return NULL;//failed to allocate memory. leave
return b; // return b (now a string)
//if (b[sizeof(b)-1] != '') // not sure what this was for
}
int main(void)
{
char *strArray = _gets(10);//creates memory for strArray
if(strArray) //use only if not NULL
{
printf("The array contains: %s", strArray );
free(strArray );
}
return 0;
}