Please help me in my program here. i started using extern pointers in my project and all the mess started. Prior to this I was simply passing double pointers to variables or returning them to propogate them from one function to other. However, usin extern, although difficult, seems very lucrative because then you don't have to worry too much about double pointers or returning stuff.
Code:
/* READER.H */
#ifndef reader_h
#define reader_h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SUCCESS 0
#define FAILURE 1
typedef struct vector_struct
{
double x, y, z;
}vector;
typedef vector vertex;
typedef struct triangle_struct
{
vector a, b, c;
vector n;
}triangle;
typedef struct object_struct
{
int nvert,ntri;
vertex *vert;
triangle *tri;
}object;
extern object *obj /* PLEASE TAKE A NOTE OF THIS ! */
int reader(object *);
#endif
if you want, you might skip this code because i have executed it before using the extern keyword and it executed fine meaning that there is nothing logically wrong with it just some problem with extern object pointer perhaps.
Code:
#include "reader.h"
int reader(object *obj)
{
enum
{
FIRST_LINE,
SECOND_LINE,
THIRD_LINE,
FOURTH_LINE,
VERTICES,
GAP,
TRIANGLES,
LAST_LINE
} state;
int vertex_count = 0;
int triangle_count = 0;
FILE *zeus_file;
char data_line[120];
state = FIRST_LINE;
zeus_file = fopen("sphere.zeus", "r");
if(zeus_file == NULL)
{
perror(" File open error!");
return FAILURE;
}
obj = malloc(sizeof(object));/* ALLOCATED MEMORY. BEFORE OBJ WAS NULL */
while (state != LAST_LINE)
{
if (fgets(data_line, 120, zeus_file) == NULL)
{
perror("zeus file read failed");
return FAILURE;
}
switch (state)
{
case (FIRST_LINE):
/* do nothing */
state = SECOND_LINE;
break;
case (SECOND_LINE):
/* do nothing */
state = THIRD_LINE;
break;
case (THIRD_LINE):
sscanf(data_line, "%d %*d %d", &obj->nvert, &obj->ntri);
obj->vert = calloc(sizeof(vertex), obj->nvert);
obj->tri = calloc(sizeof(triangle), obj->ntri);
if( obj->vert == NULL || obj->tri == NULL)
{
perror("Memory cannot be allocated!");
return FAILURE;
}
state = FOURTH_LINE;
break;
case (FOURTH_LINE):
/* do nothing */
state = VERTICES;
break;
case (VERTICES):
sscanf(data_line, "%lf %lf %lf",&obj->vert[vertex_count].x,&obj->vert[vertex_count].y,&obj->vert[vertex_count].z);
vertex_count += 1;
if (vertex_count >= obj->nvert)
state = GAP;
break;
case (GAP):
state = TRIANGLES;
break;
case (TRIANGLES):
sscanf(data_line, "%d %d %d",&obj->tri[triangle_count].v0,&obj->tri[triangle_count].v1,&obj->tri[triangle_count].v2);
triangle_count += 1;
if (triangle_count >= obj->ntri )
state = LAST_LINE;
break;
}
}
return SUCCESS;
}
Now in my main.c :
Code:
#include "reader.h"
object *obj = NULL; /* DEFINED THE EXTERN POINTER VARIABLE */
int main(void)
{
int i;
i = reader(obj)
if(i == FAILURE)
printf(" program failed \n");
else
{
printf("Number of vertices: %d Number of triangles: %d", obj->nvert, obj->ntri);
/* Program is hanging here */
}
return 0;
}
please tell me what is going on.
Also, when you declare a pointer as extern, is it true that its most uptodate value will be available everywhere and the contents to which it points to are not destroyed unless the whole program(i.e. all modules) gets terminated.