Error when passing an array to a function (using struct)

I have just started to learn C at a local college and we are using Visual Studio 2005 as the compiler/debugger.

When I write my program with the variable array declared in Main.cpp, the array passes to the function and displays the results of a file read to the screen.
However, when I declare the variable array in a struct header(DataDef.h), and make the necessary changes in the Main.cpp, Counter.h and loadArray.h, the program debugs with 2 error messages that I cannot resolve.

[code]1>d:array passed to a functionmainmain.cpp(14) : error C2664: 'Counter' : cannot convert parameter 1 from 'char [2]' to 'char'
1>d:array passed to a functionmainmain.cpp(15) : error C2664: 'loadArray' : cannot convert parameter 2 from 'char [2]' to 'char [][2]' [/code]

Would someone be kind enough to examine the code and explain what error I am making and what is the correct code?

Thank you.

Cheers

[code][b]Main.cpp[/b]

#include
#include
#include "DataDef.h"
#include "Counter.h"
#include "loadArray.h"

void main()
{
FILE * CollectorsTxt;

Collectors MyCollectors[50];
int Count=0;

Count=Counter(MyCollectors[50].code,MyCollectors[50].Name);

loadArray(Count, MyCollectors[50].code,MyCollectors[50].Name);

scanf("%*c");
}
[/code]

[code][b]DataDef.h[/b]

struct Collectors
{
char code[2];
char Name[50];
float Total;
};
[/code]

[code][b]Counter.h[/b]

int Counter(char code, char Name)
{
FILE * CollectorsTxt;
int Ctr=0;
Collectors MyCollectors[50];

CollectorsTxt=fopen("Collectors.txt","r");

fscanf(CollectorsTxt,"%[^,],%[^
]%*c",MyCollectors[50].code[Ctr],MyCollectors[50].Name[Ctr]);

while(!feof(CollectorsTxt))
{
Ctr++;
fscanf(CollectorsTxt,"%[^,],%[^
]%*c",MyCollectors[50].code[Ctr],MyCollectors[50].Name[Ctr]);
}
Ctr=Ctr-1;
return Ctr;
}
[/code]

[code][b]loadArray.h[/b]

void loadArray(int Count, char code[10][2], char Name[10][20])
{
FILE * CollectorsTxt;
int Idx=0;
Collectors MyCollectors[50];

CollectorsTxt=fopen("Collectors.txt","r");

fscanf(CollectorsTxt,"%[^,],%[^
]%*c", MyCollectors[50].code[Idx],MyCollectors[50].Name[Idx]);

printf("%s, %s
",MyCollectors[50].code[Idx], MyCollectors[50].Name[Idx]);

for(Idx=0;Idx<Count;Idx++)
{
fscanf(CollectorsTxt,"%[^,],%[^
]%*c",
MyCollectors[50].code[Idx],MyCollectors[50].Name[Idx]);
printf("%s, %s
",MyCollectors[50].code[Idx], MyCollectors[50].Name[Idx]);
}
}
[/code]

Comments

  • [color=Blue]There is no need to pass around that "[50]" thing you pass to each function. Also, the data you read from the file - how does the file look? Is it a text file, which is given to you or is it the file your code also saving? In the case of you being the creator of the file - I suggest to make this file as a binary file, so your reading/writing will be much easier than to use fscanf and feof.
    [/color]
  • This post has been deleted.
  • I apologise for not responding earlier, but I have been busy completing assignments for other modules of my course.

    After reviewing my code, I've discovered the "beginner errors" I made and the program now works.

    Thank you for your help.


    Cheers
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories