本文介绍了结构数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的程序中有这样的结构 - typedef vector vectorstruct { double x,y,z ; }向量; typedef struct verticesstruct { vector v; }顶点; / *一个顶点是一个向量* / typedef struct triangletruct { int v0,v1,v2; }三角形; typedef struct objectstruct { int nvert; int ntri; vertex * vert; triangle * tri; }对象; ................ ............... 稍后在程序的某个地方我有这样的声明 - object * obj; obj-> vert = malloc (nvert * sizeof(vertex)); / *创建一个 顶点数组* / obj-> tri = malloc(ntri * sizeof(triangle)); for(i = 0; i< obj-> nvert; i ++)/ *试图获取每个顶点的输入 其中包含x,y,z分量是一个向量* / scanf("%f%f%f",& obj-> vert [i] .x,& obj-> vert [i] .y ,& obj-> vert [i] .z); ^^^^^^^^这是上面创建数组的表示法然后 正确访问元素?? 稍后我也会这样做 - for(i = 0; i< obj-> ntri; i ++) scanf("%d%d%d"& obj-> tri [i] .v0,& obj-> tri [i] .v1,& obj-> tri [i] .v2);I have structures like this in my program -typedef vector vectorstruct{double x, y,z;} vector;typedef struct verticesstruct{vector v;} vertex; /*a vertex is a vector */typedef struct trianglestruct{int v0,v1, v2;}triangle;typedef struct objectstruct{int nvert;int ntri;vertex *vert;triangle *tri;}object;...............................later somewhere in the program i have a statement like this -object *obj;obj->vert = malloc( nvert * sizeof(vertex)); /* Creating an array ofvertices */obj->tri = malloc(ntri * sizeof(triangle));for(i=0;i<obj->nvert; i++)/* trying to take input for each vertexwhich has x, y, z components as it is a vector*/scanf("%f %f %f", &obj->vert[i].x, &obj->vert[i].y, &obj->vert[i].z);^^^^^^^^ is this above notation of creating an array and thenaccessing the elements correct ??later on i also do this -for(i=0;i<obj->ntri;i++)scanf("%d %d %d", &obj->tri[i].v0, &obj->tri[i].v1, &obj->tri[i].v2);推荐答案 语法错误。 "向量';应该是结构。请尝试从您实际尝试使用的代码中尽可能复制并粘贴 ,以便可以避免错误 等。Syntax error. "vector" should be "struct". Please try to copy and pastefrom code you''ve actually tried to use as much as possible, so that errorssuch as these can be avoided. 这很好,但是......This is fine, but... .... vertex是一个有一个成员的结构。该成员名为v, 具有类型向量。....vertex is a structure with one member. This member is named "v", andhas type vector. 好​​的,所以三角形有三个成员v0,v1和v2。Okay, so a triangle has three members v0, v1, and v2. 什么是nvert?你还没有宣布它。你的意思是obj-> nvert?请 尝试从您实际尝试使用的代码中复制和粘贴尽可能多的 ,以便可以避免这些错误。What is nvert? You haven''t declared it. Did you mean obj->nvert? Pleasetry to copy and paste from code you''ve actually tried to use as much aspossible, so that errors such as these can be avoided. 见上文。See above. 顶点有一个成员。该成员的名称为v。顶点不具有任何成员x,y或z的。矢量确实。A vertex has one member. This member has a name "v". A vertex does nothave any members x, y, or z. A vector does. 数组创建没问题。The array creation is okay. 访问权限不合适。The access is not okay. 三角形有成员v0,v1和v2,你正在访问成员v0, v1和v2。毫不奇怪,这是有效的。 :)A triangle has members v0, v1, and v2, and you''re accessing members v0,v1, and v2. Not surprisingly, this works. :) printf("%f%f%f",obj-> vert [i] .vx,obj-> vert [i] .vy, obj-printf("%f %f %f", obj->vert[i].v.x, obj->vert[i].v.y, obj- } for(i = 0; i< obj-> ntri; i ++) { fscanf(fp,"%d%d%d"&(obj-> tri [i] .v0),&(obj-> tri [ i] .v1),&(obj-}for(i=0;i<obj->ntri;i++){fscanf(fp, "%d %d %d",&(obj->tri[i].v0), &(obj->tri[i].v1), &(obj- printf("%d%d%d",obj-> tri [i] .v0,obj-> tri [i] .v1, obj-> tri [i] .v2); } } 返回1; } int main() { object * obj; char * s; clrscr(); obj = malloc(sizeof(object)); strcpy(s,& ; sphere.dat"); if(!(read_dat_file(s,obj))) printf(" unsuccessful"); 返回0; }printf("%d %d %d", obj->tri[i].v0, obj->tri[i].v1, obj->tri[i].v2);}}return 1;}int main(){object *obj;char *s;clrscr();obj = malloc(sizeof(object));strcpy(s, "sphere.dat");if(!(read_dat_file(s , obj)))printf("unsuccessful");return 0;} 这篇关于结构数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 20:00