正如在c中解释的那样
As interpreted this for in c
本文关键字:解释 更新时间:2023-10-16
我得到了这个问题是什么,我只看到了实现
for(i=0;nlist[i].nid!=0;i++)
{}
nlist[i].nid=n_id;
nlist[i].add=temp;
nlist是结构类型
的数组。结构
struct node
{
int id;
struct node *l[MAX_CONN+1];
int cost[MAX_CONN+1];
struct node **next;
int *mincost;
};
typedef struct node nodes;
struct record
{
int nid;
struct node *add;
};
typedef struct record records;
records nlist[MAX_NODES+1]={0};
全功能
FILE *f; int d; int i=0,j=0,n_id,n_cost; nodes *temp=0,*temp1=0; if((f=fopen("graph.txt","r"))== NULL) { printf("Error opening file.n"); exit(1); } memset(nlist, 0, sizeof(struct record) * MAX_NODES); count=0; do /*first get the id and address of all nodes*/ { fscanf(f,"%d",&n_id); for(i=0;nlist[i].nid!=0;i++) { if(n_id==nlist[i].nid) { printf("Id already exists."); return; } } temp=(nodes *)malloc(sizeof(nodes)); if (temp == 0) { printf("ERROR: Out of memoryn"); return; } memset(temp, 0, sizeof(struct node)); temp->id=n_id; temp->l[MAX_CONN+1]=0; temp->cost[MAX_CONN+1]=0; for(i=0;nlist[i].nid!=0;i++) {} nlist[i].nid=n_id; nlist[i].add=temp; count++; while((d=fgetc(f)!=';')) {} }while((d=fgetc(f))!=EOF); rewind(f); for(i=0;i<count;i++) /*now get the information of all nodes connections.*/ { fscanf(f,"%*d"); temp=nlist[i].add; while((d=fgetc(f)!=';')) { fscanf(f,"%d-%d",&n_id,&n_cost); for(j=0;nlist[j].nid!=0;j++) { if(nlist[j].nid==n_id) { temp1=nlist[j].add; break; } } for(j=0;temp->cost[j]!=0;j++) {} temp->cost[j]=n_cost; temp->l[j]=temp1; } } fclose(f);
缩进是误导。应该像这样缩进:
for(i=0;nlist[i].nid!=0;i++) {
/* empty loop */
}
nlist[i].nid=n_id;
nlist[i].add=temp;
我认为它应该做的是:for
循环从0推进i
,直到它找到nlist
的一个元素,它的nid
字段有一个空值。然后将该索引的nid
和add
字段分别设置为n_id
和temp
。
i
超过nlist
的末尾而没有找到合适的插槽,则程序很可能崩溃或行为失常。
相关文章:
- 请解释"函数1(p1,p2,p3);"的输出
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 被解释为低级别const的const对象的地址
- 计算每个节点的树高,帮助我解释这个代码解决方案
- MSVC将仅移动结构参数解释为指针
- 内联程序集printf将整数解释为地址
- 有人能解释一下为什么下界是这样工作的吗C++的
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 有人能为我解释一下C++代码吗
- 你能解释一下什么运行时错误是如何解决它的吗?
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- 有人可以向我解释为什么控制台输出 0 吗?
- 有人可以解释一下这段代码如何能够反转字符串
- 在 C++ 中解释多维向量的语句时感到困惑
- 解释一下 for (char c : str) 的作用?
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- 隐式重新解释引用时强制转换,没有警告/错误
- 有人可以在 c++ 中解释数组的这种输入
- 有人可以解释一下这个矩阵幂函数是如何工作的吗?