[英]Why does printf leave some random characters in the main function? [C program]
I want to get info of someone in the getInfo function.我想在 getInfo 函数中获取某人的信息。
If I print the name when I'm IN the getInfo function, it prints it out correctly, but after that in the main it leaves some random characters.如果我打印的名字时,我是getinfo功能,它正确地打印出来,但之后在主人们留下了一些随机字符。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getLine(){
char *line = malloc(256);
fgets(line, 256, stdin);
if ((strlen(line) > 0) && (line[strlen (line) - 1] == '\n'))
line[strlen (line) - 1] = '\0';
return line;
}
void getInfo(char* name,char* place,char* days[]){
printf("Type in your name:\n");
name = getLine();
printf("%s",name); // <---- THIS PRINTS OUT CORRECTLY
}
int main( int argc, const char* argv[] )
{
char* name = (char*)malloc(100*sizeof(char));
char* place = (char*)malloc(100*sizeof(char));
char* days[5];
getInfo(name,place,days);
printf("%s\n",name); // <----- THIS LEAVES SOME CHARACTERS
}
OUTPUT:输出:
C:\Users\Felhasznalo\Desktop>a.exe
Type in your name:
Peter Smith
Peter Smith`\┴
These " `\\┴ " characters are what I'm talking about.这些“`\\┴”字符就是我所说的。 The first print in the function prints it well, but in the main it gives these ghost characters.函数中的第一个打印效果很好,但主要是给出了这些幽灵字符。
Why are these written out?为什么要写出这些? Thanks.谢谢。
The problem is that you are replacing the 'name' pointer that your main
function passes to getInfo()
.问题是您正在替换main
函数传递给getInfo()
的“名称”指针。 This is because getInfo()
calls getLine()
, which itself creates a new character array, and returns a pointer to this buffer to getInfo()
.这是因为getInfo()
调用getLine()
,它本身创建一个新的字符数组,并将指向该缓冲区的指针返回给getInfo()
。
Inside getInfo()
, this is printed correctly (as you note);在getInfo()
,这是正确打印的(如您getInfo()
); however, the main
function passes a copy of the name
pointer (ie by value ) - and this is not (cannot) be changed by the getInfo
function.但是, main
函数传递name
指针的副本(即按值) - 这不是(不能)由getInfo
函数更改。 So, in main
you are printing out an uninitialized character array ( name
).因此,在main
您正在打印一个未初始化的字符数组( name
)。
There are several ways to fix this.有几种方法可以解决这个问题。 One would be to pass the name
(and, presumably, place
) argument(s) to getInfo()
as a pointer to a pointer , when you will need to de-reference this inside that function;一种方法是将name
(以及,大概是place
)参数作为指向指针的指针传递给getInfo()
,当您需要在该函数中取消引用它时; like so:像这样:
void getInfo(char** name, char** place, char* days[])
{
printf("Type in your name:\n");
*name = getLine();
printf("%s", *name);
}
Then, in your main
, you: (a) don't need to allocate memory for name
(and place
), as this is done by the getLine()
function;然后,在您的main
,您: (a) 不需要为name
(和place
)分配内存,因为这是由getLine()
函数完成的; (b) pass the address of the name
pointer to getInfo()
. (b) 将name
指针的地址传递给getInfo()
。 Like this, for example:像这样,例如:
int main(int argc, const char* argv[])
{
char* name;// = (char*)malloc(100 * sizeof(char));
char* place;// = (char*)malloc(100 * sizeof(char));
char* days[5]; // Don't know what you want to do with this!
getInfo(&name, &place, days);
printf("%s\n", name);
// Don't forget to free the allocated buffers:
free(name);
// free(place);
return 0;
}
Feel free to ask for further clarification and/or explanation.随时要求进一步澄清和/或解释。
name = getLine();
in getInfo()
does not affect name
in main()
. getInfo()
中的name
不影响main()
name
。
Try returning name
from getinfo()
and in main()
assign char *name = getline(...)
.尝试从getinfo()
返回name
并在main()
分配char *name = getline(...)
。
Aside: Alternative code for getline()
旁白: getline()
替代代码
char* getLine(){
char buffer[256];
if (fgets(buffer, sizeof buffer, stdin) == NULL) {
return NULL;
}
size_t len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') {
buffer[--len] = '\0';
}
char *line = malloc(len + 1);
if (line) {
memcpy(line, buffer, len + 1);
}
return line;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.