Im working on a project for school and I can't pass a dynamic array of structs to another function in c. The function is just supposed to check an element of the struct and return alreadyadded if that element of the array is equal to the current one. Im also having trouble declaring the function that accepts the calloc array. Any help would be appreciated! Ive been looking for a solution for this all night.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
struct EDGETAG;
typedef struct
{
char c;
bool isVisited;
struct EDGETAG* p;
} VERTEX;
typedef struct EDGE
{
VERTEX* v;
struct EDGETAG* q;
} EDGE;
int main(int argc, char* argv[])
{
int a;
struct VERTEX *vert = (VERTEX*)calloc(100, sizeof (VERTEX*));
char s;
int count = 0;
FILE* input = fopen(argv[1],"r");
while((a = fgetc(input)) != EOF)
{
if(isspace(a)==0)
{
s = a;
printf("%c ",s);
determiner(s,vert,count);
count++;
}
}
return 0;
}
and the called function
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
typedef struct VERTEX
{
char c;
bool isVisited;
struct EDGETAG* p;
} VERTEX;
typedef struct EDGETAG
{
VERTEX* v;
struct EDGETAG* q;
} EDGE;
void determiner (char a, struct VERTEX *vert, int count)
{
int i;
for(i=0;i < count; i++)
{
if(vert[i].c == a)
{
printf("%c allready added ",vert[i].c);
return ;
}
else
{
VERTEX* new1 = (VERTEX*)malloc(sizeof(VERTEX));
new1->c = a;
vert[i] = *new1;
}
}
return ;
input is: A B B C E X C D A C output:A B B B allreadyadded C E X C D A C
You never actually allocate any vertexes.
struct VERTEX *vert = (VERTEX*)calloc(100, sizeof (VERTEX*));
Notice the sizeof(VERTEX*)
? You've allocated enough space for 100 pointers to vertexes!