Thread: Error when passing Array to a function using struct.h

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Question Error when passing Array to a function using struct.h

    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(The program worked).

    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.
    1>d:\array passed to a function\main\main.cpp(14) : error C2664: 'Counter' : cannot convert parameter 1 from 'char [2]' to 'char'
    1>d:\array passed to a function\main\main.cpp(15) : error C2664: 'loadArray' : cannot convert parameter 2 from 'char [2]' to 'char [][2]'

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

    Thank you.




    Code:
    Main.cpp
    
    #include <stdio.h>
    #include <stdlib.h>
    #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:
    Counter.h
    
    int Counter(char code, char Name)
    {
    	FILE * CollectorsTxt;
    	int Ctr=0;
    	Collectors MyCollectors[50];
    
    	CollectorsTxt=fopen("Collectors.txt","r");
    
    	fscanf(CollectorsTxt,"%[^,],%[^\n]%*c",MyCollectors[50].code[Ctr],MyCollectors[50].Name[Ctr]);
    
    	while(!feof(CollectorsTxt))
    	{
    		Ctr++;
    		fscanf(CollectorsTxt,"%[^,],%[^\n]%*c",MyCollectors[50].code[Ctr],MyCollectors[50].Name[Ctr]);
    	}
    	Ctr=Ctr-1;
    	return Ctr;
    }
    Code:
    loadArray.h
    
    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,"%[^,],%[^\n]%*c",			MyCollectors[50].code[Idx],MyCollectors[50].Name[Idx]);
    
    	printf("%s, %s\n",MyCollectors[50].code[Idx],		MyCollectors[50].Name[Idx]); 
    
    	for(Idx=0;Idx<Count;Idx++)
    	{
    		fscanf(CollectorsTxt,"%[^,],%[^\n]%*c",
    MyCollectors[50].code[Idx],MyCollectors[50].Name[Idx]);
    		printf("%s, %s\n",MyCollectors[50].code[Idx],	MyCollectors[50].Name[Idx]);
    	}
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    there are many problems with your code stemming from a common misconception about arrays.

    your counter function doesn't expect an array of chars, it expects a char

    you are also trying to pass down the 51st element of an array of 50 collectors (C indexes start at 0) which doesn't exist
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Passing a 2 dimension array to a function
    By Chuck in forum C++ Programming
    Replies: 8
    Last Post: 12-09-2002, 09:42 AM