二重指针出现错误,求解啊
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <malloc.h>
# include <conio.h>
# define MAX_VERTEX_NUM 200
# define MAX_ARC_NUM 2000
# define OK 1
typedef int VertexType;
typedef int InfoType;
typedef struct cities
{
int Locate;
char country[256];
char city[256];
double Latitude;
double Longtitude;
}CityNode;
typedef struct routes
{
char FromCity[256];
char ToCity[256];
char Transport[256];
double Time;
double Cost;
double Weight;
}RouteNode;
typedef struct ArcNode//define structure ALGraph
{ int adjvex;
struct ArcNode *nextarc;
RouteNode *info;
}ArcNode;
typedef struct VNode
{ CityNode data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct
{ AdjList vertices;
int vexnum,arcnum;
int kind;
}ALGraph;
int CreateDG(ALGraph &G){ //CreateDG() subfunction int IncInfo,i,j,k,v1,v2
int v1,v2;
FILE* fp1=NULL;
fp1 = fopen ("../cities.txt","r");
if(!fp1){
printf("Can not open the file cities.csv!\n");
exit (0);
}
G.vexnum = 199;
G.arcnum = 1974;
char* str=NULL;
int L=0;
for(int i=0;i<G.vexnum;++i)
{
G.vertices[i].data.Locate = i; //initial G.vertices[i].data
str = (char*) malloc(sizeof(char) * 100);
fgets(str,99,fp1);
L=strlen(str);
for (int j=0;j<L;j++) {
if(str[j]==',')str[j]=' ';
}
sscanf(str,"%s %s %lf %lf",G.vertices[i].data.country,G.vertices[i].data.city,
&G.vertices[i].data.Latitude,&G.vertices[i].data.Longtitude);
G.vertices[i].firstarc=NULL;//initial G.vertices[i].firstarc
}
FILE* fp2=NULL;
fp2 = fopen ("../routes.txt","r");
if(!fp2){
printf("Can not open the file routes.csv!\n");
exit (0);
}
char from[256];
char to[256];
char tranpt[128];
double time,cost;
ArcNode **p; //定义二重指针
p=(ArcNode **)malloc(G.arcnum*sizeof(ArcNode*));
for(i=0;i<G.arcnum;++i)//input arc(v1,v2)
{
str = (char*) malloc(sizeof(char) * 100);
fgets(str,99,fp2);
L=strlen(str);
for (int j=0;j<L;j++) {
if(str[j]==',')str[j]=' ';
}
sscanf(str,"%s %s %s %lf %lf %lf",from,to,tranpt,&time,&cost);
int k=0;
while(strcmp(from,G.vertices[k].data.city)&&k<G.vexnum){
k++;
}
v1 = G.vertices[k].data.Locate;
k=0;
while(strcmp(to,G.vertices[k].data.city)&&k<G.vexnum){
k++;
}
v2 = G.vertices[k].data.Locate;
v1--;
v2--;
p[i]=(ArcNode *)malloc(sizeof(ArcNode));//allocate memory
if(!p)
{
printf("Overflow!\n");
return (0);
}
p[i]->adjvex=v2;//assign p->adjvex
p[i]->nextarc=G.vertices[v1].firstarc;
strcpy(p[i]->info->FromCity,from); //调试至此出现AccessViolation !求解!!!
strcpy(p[i]->info->ToCity,to);
strcpy(p[i]->info->Transport,tranpt);
p[i]->info->Time = time;
p[i]->info->Cost = cost;
p[i]->info->Weight = 0.3*(p[i]->info->Time)+0.7*(p[i]->info->Cost);//权重按照时间与费用三七开
G.vertices[v1].firstarc=p[i];
} //for end
return (OK);
} //CreateDG() end
[解决办法]
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。
[解决办法]
帮你把代码整理下格式,你那个cities.csv文件我没有,没法帮你调试噢
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 200
#define MAX_ARC_NUM 2000
#define OK 1
typedef int VertexType;
typedef int InfoType;
typedef struct cities
{
int Locate;
char country[256];
char city[256];
double Latitude;
double Longtitude;
} CityNode;
typedef struct routes
{
char FromCity[256];
char ToCity[256];
char Transport[256];
double Time;
double Cost;
double Weight;
} RouteNode;
typedef struct ArcNode //define structure ALGraph
{
int adjvex;
struct ArcNode *nextarc;
RouteNode *info;
} ArcNode;
typedef struct VNode
{
CityNode data;
ArcNode *firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum,arcnum;
int kind;
} ALGraph;
int CreateDG(ALGraph &G)
{
//CreateDG() subfunction int IncInfo,i,j,k,v1,v2
int v1,v2;
FILE* fp1=NULL;
fp1 = fopen ("../cities.txt","r");
if(!fp1)
{
printf("Can not open the file cities.csv!\n");
exit (0);
}
G.vexnum = 199;
G.arcnum = 1974;
char* str=NULL;
int L=0;
int i;
for(i=0;i<G.vexnum;++i)
{
G.vertices[i].data.Locate = i; //initial G.vertices[i].data
str = (char*) malloc(sizeof(char) * 100);
fgets(str,99,fp1);
L=strlen(str);
for (int j=0;j<L;j++)
{
if(str[j]==',')
{
str[j]=' ';
}
}
sscanf(str,"%s %s %lf %lf",G.vertices[i].data.country,G.vertices[i].data.city,
&G.vertices[i].data.Latitude,&G.vertices[i].data.Longtitude);
G.vertices[i].firstarc=NULL;//initial G.vertices[i].firstarc
}
FILE* fp2 = NULL;
fp2 = fopen ("../routes.txt","r");
if(!fp2)
{
printf("Can not open the file routes.csv!\n");
exit (0);
}
char from[256];
char to[256];
char tranpt[128];
double time,cost;
ArcNode **p; //定义二重指针
p=(ArcNode **)malloc(G.arcnum*sizeof(ArcNode*));
for(i=0;i<G.arcnum;++i)//input arc(v1,v2)
{
str = (char*) malloc(sizeof(char) * 100);
fgets(str,99,fp2);
L=strlen(str);
for (int j=0;j<L;j++)
{
if(str[j]==',')
{
str[j]=' ';
}
}
sscanf(str,"%s %s %s %lf %lf",from,to,tranpt,&time,&cost);
int k=0;
while(strcmp(from,G.vertices[k].data.city) && k<G.vexnum)
{
k++;
}
v1 = G.vertices[k].data.Locate;
k=0;
while(strcmp(to,G.vertices[k].data.city)&&k<G.vexnum)
{
k++;
}
v2 = G.vertices[k].data.Locate;
v1--;
v2--;
p[i]=(ArcNode *)malloc(sizeof(ArcNode));//allocate memory
if(!p)
{
printf("Overflow!\n");
return (0);
}
p[i]->adjvex=v2; //assign p->adjvex
p[i]->nextarc=G.vertices[v1].firstarc;
strcpy(p[i]->info->FromCity,from); //调试至此出现AccessViolation !求解!!!
strcpy(p[i]->info->ToCity,to);
strcpy(p[i]->info->Transport,tranpt);
p[i]->info->Time = time;
p[i]->info->Cost = cost;
p[i]->info->Weight = 0.3*(p[i]->info->Time)+0.7*(p[i]->info->Cost);//权重按照时间与费用三七开
G.vertices[v1].firstarc=p[i];
} //for end
return (OK);
} //CreateDG() end
int main(int argc, char *argv[])
{
ALGraph G;
CreateDG(G);
return 0;
}
[解决办法]
p[i]=(ArcNode *)malloc(sizeof(ArcNode)); //allocate memory
if(!p) //上面是对p[i]申请空间,这儿为什么变成了对p的判断,应该是if (!p[i])吧,或者if (p[i] == NULL)
{
printf("Overflow!\n");
return (0);
}