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

查询结构数组错误

2013-08-09 
查询结构数组异常// Test.cpp : Defines the entry point for the console application.//#include stdaf

查询结构数组异常

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "string"

typedef struct _student
{
char *name;
int  stdNum;
int  age;
char *birthday;
char *address;
char *Other;
}student,*pStudent;

char *searchStudent(char *name);
char *cc = (char*)malloc(1024);
int _tmain(int argc, _TCHAR* argv[])
{
printf("%s\n",searchStudent("张三"));
free(cc);
system("pause");
return 0;
}
char *searchStudent(char *name)
{
student std[3];
std[0].name ="张三";
std[0].stdNum = 1;
std[0].age = 23;
std[0].birthday="1989.12.6";
std[0].address="ABC";
std[0].Other = "好学生";

std[1].name ="李四";
std[1].stdNum = 3;
std[1].age = 24;
std[1].birthday="1989.12.6";
std[1].address="ABdff";
std[1].Other = "优干";

std[2].name = "王五";
std[2].stdNum = 4;
std[2].age = 24;
std[2].birthday="1988.11.24";
std[2].address="ABdfffd";
std[2].Other = "乐于助人";

memset(cc,0,1024);

for(int i = 0;i<3;i++)
{
if(strcmp(std[i].name,name)==0)
{
//  cc = "你所查询的信息为:";
//memset(cc,0,1024);   //b 点
printf("%s\n",std[i].name);
sprintf_s(cc,1024,"%s\n",std[i].name);  //a点                printf("%s\n",cc);
sprintf_s(cc,1024,"查询结果:姓名:%s.\n",std[i].name);
sprintf_s(cc,1024,"查询结果:姓名:%s. 学号:%d. 年龄:%d. 生日:%s.  地址:%s.  其它:%s\n",std[i].name,std[i].stdNum,std[i].age,std[i].birthday,std[i].address,std[i].Other);
printf("%s\n",cc);
break;
}
else
{
cc = "查询信息失败";
}
}
return cc;
}
(1)查询张三的信息成功,但是查询李四和王五的信息失败。报错行:sprintf_s(cc,1024,"%s\n",std[i].name);  //a点
(2)//memset(cc,0,1024);   //b 点
这行若不注释,运行时为什么要崩溃? 结构查询,报错


[解决办法]
第一个问题:

char *cc = (char*)malloc(1024);

这条语句属于一个函数调用,函数调用需要在函数里进行,这儿在全局变量里不可以这样调用的。

//#include "stdafx.h"
//#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _student
{
char *name;
int  stdNum;
int  age;
char *birthday;
char *address;
char *Other;
}student,*pStudent;

char *searchStudent(char *name);
char *cc = NULL;// = (char*)malloc(1024);
int main(int argc, char* argv[])
{
printf("%s\n",searchStudent("张三"));
free(cc);
//system("pause");
return 0;
}
char *searchStudent(char *name)
{
cc = (char*)malloc(1024);
student std[3];
std[0].name ="张三";
std[0].stdNum = 1;
std[0].age = 23;
std[0].birthday="1989.12.6";
std[0].address="ABC";
std[0].Other = "好学生";

std[1].name ="李四";
std[1].stdNum = 3;
std[1].age = 24;
std[1].birthday="1989.12.6";
std[1].address="ABdff";
std[1].Other = "优干";

std[2].name = "王五";
std[2].stdNum = 4;
std[2].age = 24;
std[2].birthday="1988.11.24";
std[2].address="ABdfffd";
std[2].Other = "乐于助人";

memset(cc,0,1024);
int i;
for(i = 0; i < 3; i++)
{
if(strcmp(std[i].name,name)==0)
{
//   cc = "你所查询的信息为:";
// memset(cc,0,1024);   //b 点
printf("%s\n", std[i].name);
snprintf(cc, 1024,"%s\n", std[i].name);  //a点
            printf("%s\n",cc);
snprintf(cc,1024,"查询结果:姓名:%s.\n",std[i].name);
snprintf(cc,1024,"查询结果:姓名:%s. 学号:%d. 年龄:%d. 生日:%s.  地址:%s.  其它:%s\n",std[i].name,std[i].stdNum,std[i].age,std[i].birthday,std[i].address,std[i].Other);
printf("%s\n",cc);
break;
}
else
{
cc = "查询信息失败";
}
}
return cc;
}

这个是C语言版的,你把上面的那句放在函数里调用试一试。
[解决办法]
这个代码太。。。。。
[解决办法]
// memset(cc,0,1024);   //b 点 这行若不注释,运行时为什么要崩溃? 
这句只是赋值操作,应该不是这句导致崩溃的吧!


重点代码贴下,你的太乱了阿!又没说明


[解决办法]
没啥大毛病那!

#include <stdio.h>
#include <malloc.h>
typedef struct _student
{
char *name;
int  stdNum;
int  age;
char *birthday;
char *address;
char *Other;
}student,*pStudent;

char *searchStudent(char *name);

char *cc;

int main(int argc, char* argv[])
{
cc = (char *)malloc(1024);
printf("%s\n",searchStudent("张三"));
free(cc);
system("pause");
return 0;
}
char *searchStudent(char *name)
{
student std[3];
std[0].name ="张三";
std[0].stdNum = 1;
std[0].age = 23;
std[0].birthday="1989.12.6";
std[0].address="ABC";
std[0].Other = "好学生";

std[1].name ="李四";
std[1].stdNum = 3;
std[1].age = 24;
std[1].birthday="1989.12.6";
std[1].address="ABdff";
std[1].Other = "优干";

std[2].name = "王五";
std[2].stdNum = 4;
std[2].age = 24;
std[2].birthday="1988.11.24";
std[2].address="ABdfffd";
std[2].Other = "乐于助人";

memset(cc,0,1024);

int i;
for(i = 0;i<3;i++)
{
if(strcmp(std[i].name,name)==0)
{
printf("%s\n",std[i].name);
sprintf(cc,"%s\n",std[i].name);  //a点                 printf("%s\n",cc);
sprintf(cc,"查询结果:姓名:%s.\n",std[i].name);
sprintf(cc,"查询结果:姓名:%s. 学号:%d. 年龄:%d. 生日:%s.  地址:%s.  其它:%s\n",std[i].name,std[i].stdNum,std[i].age,std[i].birthday,std[i].address,std[i].Other);
printf("%s\n",cc);
break;
}
else
{
cc = "查询信息失败";
}
}
return cc;
}

热点排行