C++ Primer 3rd P305 练习7.14程序调试问题
来源:百度知道 编辑:UC知道 时间:2024/05/15 05:00:40
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *str = "hello";
extern "C"
{
void *malloc(int);
char *strcpy(char *, const char *);
int printf(const char *, ...);
int exit(int);
int strlen(const char *);
}
int main()
{
char *s = (char *)malloc(strlen(str) + 1);
strcpy(s, str);
printf("%s, world\n", s);
return 0;
}
以下为报错消息:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *str = "hello";
extern "C"
{
void *malloc(int);
char *strcpy(char *, const char *);
int printf(const char *, ...);
int exit(int);
int strlen(const char *);
}
int main()
{
char *s = (char *)malloc(strlen(str) + 1);
strcpy(s, str);
printf("%s, world
#include <stdlib.h>
#include <string.h>
const char *str = "hello";
extern "C"
{
void *malloc(int);
char *strcpy(char *, const char *);
int printf(const char *, ...);
int exit(int);
int strlen(const char *);
}
int main()
{
char *s = (char *)malloc(strlen(str) + 1);
strcpy(s, str);
printf("%s, world\n", s);
return 0;
}
以下为报错消息:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *str = "hello";
extern "C"
{
void *malloc(int);
char *strcpy(char *, const char *);
int printf(const char *, ...);
int exit(int);
int strlen(const char *);
}
int main()
{
char *s = (char *)malloc(strlen(str) + 1);
strcpy(s, str);
printf("%s, world
头文件和extern内容重复.造成overloading.
解决一:
const char *str = "hello";
extern "C"
{
void *malloc(int);
char *strcpy(char *, const char *);
int printf(const char *, ...);
int exit(int);
int strlen(const char *);
}
int main()
{
char *s = (char *)malloc(strlen(str) + 1);
strcpy(s, str);
printf("%s, world\n", s);
return 0;
}
解决二:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *str = "hello";
int main()
{
char *s = (char *)malloc(strlen(str) + 1);
strcpy(s, str);
printf("%s, world\n", s);
return 0;
}