John Smith
John Smith

Reputation: 633

Creating array of Structs

I'm constantly getting this error whenever I try to pass a array of Strucs into a function.

10: error: expected declaration specifiers or ‘...’ before ‘RECORD’ 113: error: conflicting types for ‘edit’ 10: error: previous declaration of ‘edit’ was here

I also get a lot of warnings about how I use strlen and strcmp about the arguments and how I'm making a pointer from a int without a cast. Is there anything wrong with this?

Can anyone explain to me what's wrong with the code I have so far?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLENGTH 51 //longest length of a single record + 1 =51 bytes
#define SIZEOFDB 1000
#define ENCRYPT 5

void add(int argc, char *argv[]);
void del (char *username);
void edit(char *nName, char *nPw, char *nType, char *tName, char *tPw, RECORD *arr);
int verify (char *username, char *password);
void parse (char *record, char *delim, char);

typedef struct rec
{
char name[22];
char pw[22];
char type[6];
}RECORD;

int main(int argc, char *argv[]){
int j;
char tempName[22];
char tempPw[22];
char tempType[6];
char newName[22];
char newPw[22];
char newType[6];
static const char filename[] = "password.csv";
FILE *file;
file = fopen(filename, "r+");

if (file == NULL){
    //create csv file password.csv
    printf("Just created password.csv");
    file = fopen(filename, "w+");
}
size_t i =0, count, size = 1000;
RECORD *arr = malloc(size * sizeof *arr);
if (arr){
    char line[51];
    while(i<size && fgets(line, sizeof(line), file)){
        sscanf(line, "%[^','],%[^','],%s", &arr[i].name, &arr[i].pw, &arr[i].type);
        ++i;
    }

    fclose(file);

    if (strcmp(argv[1], "-menu") == 0){
        //menu function
        printf("MENU\n");
    } else if(strcmp( argv[1], "-add") == 0){
        //add function

        strncpy(newName,argv[2],strlen(argv[2]));
        strncpy(newPw,argv[3],strlen(argv[3]));
        strncpy(newType,argv[4], strlen(argv[4]));

        printf("ADD\n");
    } else if(strcmp( argv[1], "-del") == 0){
        //del function

        strncpy(tempName,argv[2], strlen(argv[2]));

        printf("DEL\n");
    } else if(strcmp( argv[1], "-edit") == 0){
        //edit funciton

        strncpy(tempName,argv[2],strlen(argv[2]));
        strncpy(tempPw,argv[3],strlen(argv[3]));
        strncpy(tempType,argv[4],strlen(argv[4]));
        strncpy(newName,argv[5], strlen(argv[5]));
        strncpy(newPw,argv[6], strlen(argv[6]));
        strncpy(newType, argv[7], strlen(argv[7]));

        printf("EDIT\n");
    } else if(strcmp( argv[1], "-verify") == 0){
        //verify function

        strncpy(tempName,argv[2],strlen(argv[2]));
        strncpy(tempPw,argv[3],strlen(argv[3]));

        printf("VERIFY\n");
    } else {
        printf("SYNTAX ERROR\n");
        return 0;
    }
    for (j = 0; j < i; j++){
        printf("%s,%s,%s\n", arr[j].name,arr[j].pw,arr[j].type);
    }
}
free (arr);



return 0;

}

/*void add(char nName[],char nPw[], char nType[], record *arr,FILE *file; ){
int i,j;
if (arr){
char line[51];
while(i<size && fgets(line, sizeof(line), file)){
sscanf(line, "%[^','],%[^','],%s", &arr[i].name, &arr[i].pw, &arr[i].type);
++i
}

}*/

void edit(char *nName,char *nPw, char *nType, char *tName, char *tPw, RECORD *arr){
int i,j;
int size = (sizeof *arr)/(sizeof arr[0]);
for (i=0; i< size; i++){

if ((strcmp( arr[i].name, *tName) == 0 && strcmp (arr[i].pw, *tPw) == 0)){
for (j=0; j<size; j++){
if (strcmp(*nName, arr[j].name) == 0){
printf("ERROR USERNAME ALREADY EXISTS");
break;
}
}
strncpy(arr[i].name, *nName, strlen(*nName));
strncpy(arr[i].pw, *nPw, strlen(*nPw));
strncpy(arr[i].type, *nType, strlen(*nType));
}
}

}

Thanks

Upvotes: 0

Views: 157

Answers (3)

arc
arc

Reputation: 584

Try putting the structure definition for "RECORD" before the function declaration for "edit"

Upvotes: 0

TimKouters
TimKouters

Reputation: 26

Try is like this:

typedef struct rec
{
char name[22];
char pw[22];
char type[6];
}RECORD;

void add(int argc, char *argv[]);
void del (char *username);
void edit(char *nName, char *nPw, char *nType, char *tName, char *tPw, RECORD *arr);
int verify (char *username, char *password);
void parse (char *record, char *delim, char);

instead of:

void add(int argc, char *argv[]);
void del (char *username);
void edit(char *nName, char *nPw, char *nType, char *tName, char *tPw, RECORD *arr);
int verify (char *username, char *password);
void parse (char *record, char *delim, char);

typedef struct rec
{
char name[22];
char pw[22];
char type[6];
}RECORD;

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 224944

You need to declare the RECORD structure before you use it in a parameter list. That is, you should move it to before the forward declarations of your functions.

Upvotes: 2

Related Questions