1 Replies - 197 Views - Last Post: 06 May 2019 - 07:28 AM Rate Topic: -----

#1 SanBuck   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-May 19

Trouble with passing arrays into functions in C

Posted 06 May 2019 - 03:51 AM

I am not sure what I am doing wrong?? It's coming up with warnings. I am trying to pass the array into the function but why is it not taking it. Can someone help me with explaining how to pass the array? Thank you

//
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int size=50;

//function declaration
int checkArg(int argc);
void checkFile(char argv[]);
void scanFile(char argv[], int size);
void printScre(char argv[], int size);

//print file to screen
void printScre(char argv[], int size)
{
	FILE *srcFile;
	int y;
	char line[size];
	int age[size];
	float wage[size];
	
	printf("in printScreen function\n");
	/*for(y=0; y < size; y++)
	{
		printf("Line %s, %d, %.2f\n", line[y],age[y], wage[y]);
	}*/
}


//scan file for details
void scanFile(char argv[], int size)
{
	FILE *srcFile;
	
	int y;
	char line[size];
	int age[size];
	float wage[size];
	
	srcFile = fopen (argv, "r"); 
	printf("in scanning function\n");
	//need to read arrays from file
	for(y=0; y < size; y++)
	{
		fscanf(srcFile, "%s %d %f\n", line[y], &age[y], &wage[y]);
		printf("Line %s, %d, %.2f\n", line[y],age[y], wage[y]);
	}
	

}
//check if file found 
void checkFile(char argv[])
{
	FILE *srcFile;
	srcFile = fopen (argv[1], "r"); 
	
	if(srcFile == NULL)
	{
		printf("File not found");
		exit(0);
	}
	else
	{
		printf("file opened succesfully\n");
	}
}
int checkArg(int argc)
{
	if(argc == 1)
	{
		printf("No command line arguments given!\n");
		exit(0);
	}
	else
	{
		printf("Command line arguments given!\n");
		return(0);
	}
}
//read command line 
int main(int argc, char *argv[])
{
		
//function calling	
checkArg(argc);	
checkFile(argv);
scanFile(argv, size);
printScre(argv, size);


return(0);

}



Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with passing arrays into functions in C

#2 jimblumberg   User is offline

  • member icon

Reputation: 5731
  • View blog
  • Posts: 17,557
  • Joined: 25-December 09

Re: Trouble with passing arrays into functions in C

Posted 06 May 2019 - 07:28 AM

Look at this snippet:

//check if file found
void checkFile(char argv[])
{
	FILE *srcFile;
	srcFile = fopen (argv[1], "r");



The fopen() function requires a C-string as an argument, you're trying to use a single char not a C-string.

Quote

It's coming up with warnings.

In future post the complete warning/error messages, all of them, exactly as they appear in your development environment.

By the way this: char line[size]; may or may not be allowed by your compiler, depending on the version of the C standard you happen to be using. This called a VLA (Variable Length Array) and is only truly supported with the C99 standard. You would be better off using a normal array of a size large enough, perhaps use your defined constant instead of the function parameter. And remember that that function parameter named size is "hiding" the global variable named size you probably want to use.

Lastly don't forget that variables defined inside a function are local to that function and cease to exist when the function returns. You probably want to pass some variables into your functions so that the values can be used outside your functions. You probably should be using a structure to hold the various elements being read from the file.

And be careful you are trying to open the same file multiple times (looks like almost every function is going to try to open the file). And remember that since you are never closing the file you may have problems as well.





Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1