【发布时间】:2015-08-31 11:41:19
【问题描述】:
我正在尝试创建一个函数,该函数将从用户那里接收 char * 并将其打印出来。
当我打印它时,它把我的价值变成了奇怪的东西。
**//input method**
char* readContactName(){
char tmp[20];
do{
printf("What is your contact name?: (max %d chars) ", MAX_LENGH);
fflush(stdin);
scanf("%s", &tmp);
} while (!strcmp(tmp, ""));
return tmp;
}
void readContact (Contact* contact)
{
char* tmp;
tmp = readContactName();
updateContactName(contact, tmp);
}
**//when entering this function the string is correct**
void updateContactName(Contact* contact, char str[MAX_LENGH])
{
printf("contact name is %s\n",&str); --> prints rubish
}
我错过了什么?
【问题讨论】:
-
你不能在 C 中返回指向局部变量的指针。编译器允许你这样做,但它不起作用。