typedefstruct {
int NumOfElt;
int *Value;
} Vector;
int ReadVector(char *fileName, Vector **vector);
void CreateVector(Vector **vector, int numOfElt);
int SetValue(Vector *vector, int index, int value);
void main()
{
FILE* f;
f=open("test","r");
Vector * vector // Declare a pointer
ReadVector(f,&vector);// is it right?
}
void CreateVector(Vector **vector, int numOfElt) {
(*vector) = malloc(sizeof(Vector));
(*vector)->NumOfElt = numOfElt;
(*vector)->Value = calloc(numOfElt, sizeof(int));
}
int ReadVector(char *fileName, Vector **vector);
{
// I read file OK.. read number from file to numOfElt, value
CreateVector(&vector, numOfElt); // is it right?
Setvalue(*vector,1,value);//is it right? 1 is a sample index.. In fact, I use for loop..
return 0;
}
int SetValue(Vector *vector, int index, int value)
{
vector->Value[index]=value;
return 0;
}
Line 4: Non-standard main(). Should be int main().
Line 7: Typo? "Vecrot" instead of "Vector"
Line 14: Bad definition. Should be int ReadVector( Vector **vector) if meant to match the declaration on line 1.
Line 16: Typo? "CreadteVector" instead of "CreateVector". Referencing a Vector ** will produce a Vector ***.
Line 19: No return.
thanks helios..
I'm so sorry.. I mistyping, but the code in the file.c is right..
I edited my first post.. please read again and give me advices.. thank you very much
Lines 7-9: These functions should be methods of Vector.
Line 17: Call doesn't match declaration of ReadVector().
Lines 22 and 24: See http://www.cplusplus.com/doc/tutorial/dynamic/ for how to dynamically allocate memory in C++. malloc() and all its variations and free() are not used anymore.
Line 2: I'm using C now... I know this is cplusplus forum, but I know that all of you know C so well. ^_^...
Lines 7-9: This is a part of my exercises, and instructor gave the structure of the code that must have these function.. ( in fact I have to write them instead of using from the library)
Line 17: again mistyping :((..
Lines 22 and 24: in C++, they use New . But I'm using C..
the Problem is when I use Linux to compile, error:
1/ Segmentation Fault ,
2/
the func Setvalue(*vector,1,value) in Readvector func , can't access the memory.. ( *vector).. And I think that I am wrong when push the parameter to the function...
Perhaps you should have said that to begin with, mmh?
Line 19: The function call still doesn't match the declaration. A FILE * is not a char *.
Your line 37, I have just noticed, still needs fixing. You're referencing vector, which is a Vector **. This produces a Vector ***, but CreateVector() takes a Vector **.
There doesn't appear to be anything wrong in SetValue(). The problem must lie somewhere else. See whether the vector you're creating is of the right size, and whether the element you're trying to set is inside the boundaries of the vector.
thanks helios so much..
my problem has been solved..
by the way, I'm sorry about mistyping.. because I have to copy from the source file to here in order to make the post easy to read..
Thanks you very much..