Hello all,
I have this struct:

typedef struct cell {
    char _name[2];
    int _calcResult;
    int _depend[MAX_CELLS];
} Cell;

and this is my main:

int main(int argc, char* argv[]) {
    FILE *fp;
    int i;
    fp=fopen(argv[1], "r");
    Cell Cells[MAX_CELLS];
    fillNames(fp,Cells); 
    for(i=0;i<4;i++)
        printf("%s\n", Cells[i]->_name);
}
static void fillNames(FILE* fp, Cell *Cells) {
    char line[2];
    int i=0;
    while(fgets(line, sizeof line, fp)!=NULL) {
        strcpy(Cells[i]->_name, line);
        i++;
    }
}

Any help will be very appreciated!!
Thanks.
I have an array of structs and I'm trying to insert the first two chars from each line from the file into the _name field in the struct inside the array.

I get this message:
error: invalid type argument of ‘->’ (have ‘Cell’)

I will appreciate it very much if someone can tell me what should I change in order to do what I meant to do.

Thanks again.

I changed the -> to .
and it compiled but I got an Segmentation fault.

Why this happens to me?

Dude, you don't need to "open" argv - it's there, just waiting for you, courtesy of the C compiler.

C automatically opens stdin, stdout, stderr, and creates argc and argv, when your program starts. (May not be an exhaustive list)

Thanks for your reply.
But I don't think thats the problem..
I wrote on the run line the text file I want to read.
I did it in another program and it works.
I guess something wrong with my code.

anyone?

Thanks for your reply.
But I don't think thats the problem..
I wrote on the run line the text file I want to read.
I did it in another program and it works.
I guess something wrong with my code.

anyone?

2 chances
1. You are not passing the argument.
to check that add

if (argc != 2)
    {
                printf("ERROR - No input file");
                return 0;
    }

to your program in the beginning before trying to open the file

2. The file you have passed doesnt exist.
To cover that scenario, add

fp=fopen(argv[1], "r");
   if (fp == NULL)
   {
        printf("Error opening file");
         return 0;
   }

I did that check.. didn't help.. :(

Your program is running fine on my system [without any segmentation fault, I dint check the output]

If possible please post the complete code here.
Otherwise try to debug yourself.

Use gdb if you are on linux and try to find the backtrace.
if your c file is testfile.c and file you are opening is file.txt,
compile using : gcc -g testfile.c
then run as : gdb a.out
under gdb prompt give (gdb) r file.txt

Then let the program crash. It will come to gdb prompt. Then give bt command to get the backtrace. It will show you where the program crashed
eg: (gdb)bt

If this does nt help, post the code or give some more details regarding the error.

Edited 6 Years Ago by sree_ec: n/a

First, your open is fine. Adak, he's using argv to open the data file.
Second, sree_ec is correct.
Third, when you use fgets() to read 2 characters, the array must be of size 3. Remember (or look it up), fgets() always adds an ending \0 and reads length-1 characters.

Thanks for the reply guys.
That didn't help. I changed the array's length to 3, still.
Ok, here is the complete code.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAX_LENGTH 1000
#define MAX_CELLS 2574
#define SUCCESS_RETURN 0
#define ERROR_RETURN 1
#define WHITE 0
#define GREY 1
#define BLACK 2


typedef struct cell {
    char _name[3];
    int _calcResult;
    int _depend[MAX_CELLS];
} Cell;


static void fillNames(FILE* fp, Cell *Cells);


int main(int argc, char* argv[]) {


    //check arguments
    if(argc!=2) {
        printf("Error. Illegal arguments.\n");
        return ERROR_RETURN;
    }
    
    FILE *fp;
    int i;
    fp=fopen(argv[1], "r");
    if(fp==NULL) {
        printf("Error. Bad file or file doesn't exist.\n");
        return ERROR_RETURN;
    }

    Cell Cells[MAX_CELLS];
    fillNames(fp, Cells);
    for(i=0;i<4;i++) {}
        printf("%s\n", Cells[i]._name);
}


/*
 * Fill the name field in the struct Cell according to the cell name.
 * (Which is the beginning of each line.
 */
static void fillNames(FILE* fp, Cell *Cells) {
    char line[3];
    int i=0;
    while(fgets(line, sizeof line, fp)!=NULL) {
        strcpy(Cells[i]._name, line);
        i++;
    }
}

Thanks for the reply guys.
That didn't help. I changed the array's length to 3, still.
Ok, here is the complete code.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAX_LENGTH 1000
#define MAX_CELLS 2574
#define SUCCESS_RETURN 0
#define ERROR_RETURN 1
#define WHITE 0
#define GREY 1
#define BLACK 2


typedef struct cell {
    char _name[3];
    int _calcResult;
    int _depend[MAX_CELLS];
} Cell;


static void fillNames(FILE* fp, Cell *Cells);


int main(int argc, char* argv[]) {


    //check arguments
    if(argc!=2) {
        printf("Error. Illegal arguments.\n");
        return ERROR_RETURN;
    }
    
    FILE *fp;
    int i;
    fp=fopen(argv[1], "r");
    if(fp==NULL) {
        printf("Error. Bad file or file doesn't exist.\n");
        return ERROR_RETURN;
    }

    Cell Cells[MAX_CELLS];
    fillNames(fp, Cells);
    for(i=0;i<4;i++) {}
        printf("%s\n", Cells[i]._name);
}


/*
 * Fill the name field in the struct Cell according to the cell name.
 * (Which is the beginning of each line.
 */
static void fillNames(FILE* fp, Cell *Cells) {
    char line[3];
    int i=0;
    while(fgets(line, sizeof line, fp)!=NULL) {
        strcpy(Cells[i]._name, line);
        i++;
    }
}

Seems like a stack overflow

I am assuming you are programming in linux [bash]
Try changing the MAX_CELLS value to a lower value [20 or 50] , see if crash is happening.
If not,
do a

$ ulimit -a

and check what is your stack size.
you will get an output similar to this


ore file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 30849
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) 3365660
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 30849
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

See if it is more than the stack size needed for Cells Array. it will atleast need MAX_CELLS * MAX_CELLS
The program will atleast need that much stack size for that local variable Cells.

If the stack size shown is less than what you need try any of the following
1. change the stack size to more than what you need by command 'ulimit -s value' eg: ulimit -s 8000000
then run the program again
2. do the command 'ulimit -c unlimited' to get the coredump if you are using linux
compile the program using -g command
run the executable. It will generate a core dump file in the folder where you are running.
run the executable again with gdb eg: gdb ./a.out <corefile>
This will show you where exactly the crash happened. If it is some arbitrary position , no worries it will be a stack corruption
3.Try moving the definition of local variable to beginning of the program so that it doesnt corrupt any of the code and run again
4.If nothing works move it out of stack, ie, change the local variable to global or reduce its size :)

Post the results after trying out these things

note: you can try 'ulimit -s unlimited ' command also. I have never tried it though

If you are using any other compiler or ide or anything, see how you can change the stack size and work it around.

Edited 6 Years Ago by sree_ec: n/a

So what's happening now? If you need help, you have to describe in detail what you need help with.

Just curious, why are you using '_' before each structure name?

sree_ec Thanks a lot, I'll try to do as you said.
WaltP I use the _ before every "data member" of the struct.. it is kind of convention.

What I'm trying to do is this:
I have a file which contains things like this:

A1: B2+5
B2: C3+2
C3: 6
N7: 3+2*B2

Each "cell" will be presented as a struct which includes its name, and the other cells it "depends" on. And I added another field which will contain the calculated result.

What I'm trying to do, at first, is to define an array of structs(which will represent the cells).. and fill the _name field in each struct in the array in its name.
So, in the example above, I'll have an array of size 4 and it will include a 4 structures.. with the names: A1, B2 .... so on.

so, what is the best way to do what I want?

MANY THANKS!!!

I forgot to say, that when I moved the array of Cells out of the main the problem solved.

Seems like a stack overflow

I am assuming you are programming in linux [bash]
Try changing the MAX_CELLS value to a lower value [20 or 50] , see if crash is happening.
If not,
do a

and check what is your stack size.
you will get an output similar to this

See if it is more than the stack size needed for Cells Array. it will atleast need MAX_CELLS * MAX_CELLS
The program will atleast need that much stack size for that local variable Cells.

If the stack size shown is less than what you need try any of the following
1. change the stack size to more than what you need by command 'ulimit -s value' eg: ulimit -s 8000000
then run the program again
2. do the command 'ulimit -c unlimited' to get the coredump if you are using linux
compile the program using -g command
run the executable. It will generate a core dump file in the folder where you are running.
run the executable again with gdb eg: gdb ./a.out <corefile>
This will show you where exactly the crash happened. If it is some arbitrary position , no worries it will be a stack corruption
3.Try moving the definition of local variable to beginning of the program so that it doesnt corrupt any of the code and run again
4.If nothing works move it out of stack, ie, change the local variable to global or reduce its size :)

Post the results after trying out these things

note: you can try 'ulimit -s unlimited ' command also. I have never tried it though

If you are using any other compiler or ide or anything, see how you can change the stack size and work it around.

edit: It will need atleast 8*MAX_CELLS * MAX_CELLS bytes considering only the last member of the structure.

Edited 6 Years Ago by sree_ec: n/a

I forgot to say, that when I moved the array of Cells out of the main the problem solved.

thats what I suggested as the 4th step. Moving the local variable to a global variable.
So you dont get stack corruption.
But before that try out options 1 and 3. It will make your understanding more clear about the problem.
Finally, you need to know how much stack your application is supposed to use depending on the purpose and the platform for which it is written.

sree_ec Thanks a lot, I'll try to do as you said.
WaltP I use the _ before every "data member" of the struct.. it is kind of convention.

What I'm trying to do is this:
I have a file which contains things like this:

A1: B2+5
B2: C3+2
C3: 6
N7: 3+2*B2

Each "cell" will be presented as a struct which includes its name, and the other cells it "depends" on. And I added another field which will contain the calculated result.

What I'm trying to do, at first, is to define an array of structs(which will represent the cells).. and fill the _name field in each struct in the array in its name.
So, in the example above, I'll have an array of size 4 and it will include a 4 structures.. with the names: A1, B2 .... so on.

so, what is the best way to do what I want?

MANY THANKS!!!

By the way I understood your problem, each line in the above file represents a structure. Then you dont need an array to represent dependency.
In the eg you gave,
Since the file has only 4 lines you are having an array of structures of size 4.
The first structure with index '0' willl have members like

._name = "A3"
 ._calcResult = 5
 .dependency = 1 // which is the index of the dependent structure . for this why you need an array here? Just one integer to represent the array index is enough , correct?

second member of array with index 1 will be

._name = "B2"
._calcResult = 2
._dependency =2 //which is the index of array representing C3

and so on...

Edited 6 Years Ago by sree_ec: n/a

Lets consider the file I wrote above again.
The array of structures will look something like this:

Cell[0] will be a structure with these fields: 
Cell[0]._name="A1"
Cell[0]._calcValue="13" (because it is B2 which is 8, plus 5)
Cell[0]._dependency="1" (this is the index of the cell B2)

and so on for the rest of the cells.

The file example:
A1: B2+5
B2: C3+2
C3: 6
N7: 3+2*B2

Lets consider the file I wrote above again.
The array of structures will look something like this:

Cell[0] will be a structure with these fields: 
Cell[0]._name="A1"
Cell[0]._calcValue="13" (because it is B2 which is 8, plus 5)
Cell[0]._dependency="1" (this is the index of the cell B2)

and so on for the rest of the cells.

The file example:
A1: B2+5
B2: C3+2
C3: 6
N7: 3+2*B2

Seems your problem is solved and you understood what the issue causing seg fault initially. If thats the case, mark this thread as solved.

This question has already been answered. Start a new discussion instead.