首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

测试代码有点有关问题,搞不明白 大神帮忙看下什么有关问题

2012-06-09 
测试代码有点问题,搞不明白 大神帮忙看下什么问题?代码一共三个文件main.c test.c test.h如下所示:/******

测试代码有点问题,搞不明白 大神帮忙看下什么问题?
代码一共三个文件
main.c test.c test.h
如下所示:

/******************************************test.h*********************************************/
#ifndef TEST_H
#define TEST_H

extern char *get_a_day( int );

#endif

/*********************************************test.c*********************************************/
#include "test.h"
 const char * msg[] ={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};

char * get_a_day( int index)
{
char pday [20];
strcpy(pday,msg[index]);
return pday;
}

/* ********************************************main.c *********************************************/
#include <stdio.h>
#include "test.h"
int main(void)

printf("%s \n%s\n", get_a_day(0), get_a_day(1));
printf("%s\n",get_a_day(2));
system("pause");
return 0;
}
输出结果为:
sunday
ay
tuesday请按任意键继续. . .

main.c这样的写的时候
/*****************************************************main.c*******************************************/
/* main.c */
#include <stdio.h>
#include "test.h"
int main(void)

printf("%s\n", get_a_day(0));
printf("%s\n",get_a_day(1));
printf("%s\n",get_a_day(2));system("pause");
return 0;
}
输出结果:
sunday
monday
tuesday请按任意键继续. . .

另外
如果test.c main.c这么实现的话 输出是这样子的:

/*********************************************test.c*********************************************/
#include "test.h"
static const char * msg[] ={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};

char * get_a_day( int index)
{
static char pday [20];
strcpy(pday,msg[index]);
return pday;
}

/*********************************************main.c*********************************************/
#include <stdio.h>
#include "test.h"
int main(void)

printf("%s\n%s\n", get_a_day(0),get_a_day(1));
printf("%s\n",get_a_day(2));system("pause");
return 0;
}
输出:
sunday
sunday
tuesday请按任意键继续. . .




[解决办法]

C/C++ code
char * get_a_day( int index){char pday [20];  // 这样定义的是临时数组,函数执行完就会释放的strcpy(pday,msg[index]);return pday;}
[解决办法]
探讨
C/C++ code

char * get_a_day( int index)
{
char pday [20]; // 这样定义的是临时数组,函数执行完就会释放的
strcpy(pday,msg[index]);

return pday;
}

[解决办法]
C/C++ code
char * get_a_day( int index){char *pday = (char *)malloc(sizeof(char) * 20);if (pday == NULL){fprintf(stderr, "malloc failed");exit(1);}strcpy(pday,msg[index]); return pday;}
[解决办法]
printf(const char* FORMAT, ...);

输出的时候, 有一个指针指向后面的参数, 在赋值的时候, printf("%s\t%s", get_a_day(0), get_a_day(1)); 你可以看看两次get_a_day分别写到哪个地址, 我这里是这样的, 
1, 在地址为0x0012fe74的位置上赋值为monday, 也就是说在地址0x0012fe74里是mond, 0x0012fe78里面放了ay(\0);

2,在地址为0x0012fe70的位置上赋值为sunday, 同理, 就是在0x0012fe70里放了sund, 0x0012fe74里放了ay(\0)(覆盖了上面放的东西);

在输出的时候, 按照%s的格式, 也就是说, 它找到第一个地址, 0x0012fe70, 然后向后读, 读到'\0', 做一个输出, 然后按照刚才的说法, 第二个参数里是0x0012fe74,也就是刚才的sunday的ay\0, 感觉上来说, 就是monday已经被毁了.



我试的时候, 用了123456789发现后面很多东西被毁了.

因为return pday没有接受的地方, 都是系统给找的.如果一次只输出一个还好, 它能一次给你开辟一个你够用的地方, 如果要一次弄很多个, 它每开辟一个, 就忘记了上一个, 我感觉貌似是这样.
[解决办法]
既然用了全局变量,就用到底吧!

C/C++ code
const char * msg[] ={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};char * get_a_day( int index){    char pday [20];    strcpy(pday,msg[index]);    return pday;} 

热点排行