7 Replies - 1603 Views - Last Post: 26 October 2010 - 12:56 AM Rate Topic: -----

#1 m_johnst  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-September 10

Pointer From Integer Without Cast Warning

Posted 25 October 2010 - 02:34 PM

Hello everyone,
I'm really unsure on how to remove this warning from my code (it appears twice):
Passing argument 2 of 'getIndex' makes pointer from integer without a cast



The warning appears on line 36 and 37 of my code:


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

#define OPEN 1
#define CLOSED 0

/*
 * Function to return index at which team ID input is stored
 */
int getIndex(char *id, char idList[50][50]) {
	int k;
	for (k=0; k<50; k++) {
		if (strcmp(id,idList[k])==0) {
			return k;
		}
	}
	fprintf(stderr,"Error in getIndex function.\n");
	return 0;
}


/* 
 * gets file pointer, reads line, determines winner of game, adds win/loss/tie
 */
void next_game(FILE *fp, char teamIDs[50][50], int *teamWins, int *teamLosses, int *teamTies) {
		
	/* team IDs from file */
	char team1[50];
	char team2[50];
	
	/* scores for each team from file */
	int score1;
	int score2;
	
	/* index in array for each team */
	int index1 = getIndex(team1,teamIDs[50][50]);
	int index2 = getIndex(team2,teamIDs[50][50]);

	/* 
	 * gets line from file with both teams IDs and scores
	 * determines winner/tie and increments value in array at according index
	 */
	if (fscanf(fp,"%s %s %d %d",team1,team2,&score1,&score2)==4) {
		if (score1 > score2) {
			teamWins[index1]++;
			teamLosses[index2]++;
		}
		else if (score1 < score2) {
			teamWins[index2]++;
			teamLosses[index1]++;
		}
		else {
			teamTies[index1]++;
			teamTies[index2]++;
		}
	}
	else { /* No more input in file */
		fclose(fp);
	}
}

/*
 * Function to print out league summery from arrays in columns
 * Takes file that links team ID with team name
 */
void arrayPrint(FILE* fp, int win[], int loss[], int tie[]) {
	
	char id[50];
	char name[25];
	
	int i=0;
	
	/* prints columns */
	printf("<TEAM NAME>\t<NUMBER OF WINS>\t<NUMBER OF LOSSES>\t<NUMBER OF TIES>\t<WINNING PERCENTAGE>\n");
	while (fscanf(fp,"%s %s",id,name)==2) {
		printf("%s/t%d/t%d/t%d/t%d%%\n",name,win[i],loss[i],tie[i],(win[i]/loss[i])*100);
		i++;
	}
}


int main(int argc, char* argv[]) {
	
	/* Displays error message if 2 files are not included in command line */
	if (argc%3 != 0) {
		fprintf(stderr,"Error in %s main function. Must take 2 files as input.\n",argv[0]);
		return -1;
	}
	
	/* stores each team's wins/losses/ties in a specific index */
	int teamWins[50] = {0};
	int teamLosses[50] = {0};
	int teamTies[50] = {0};
	
	/* stores team names (index for team name is same for index for that team's win/loss/tie record) */
	char teamIDs[50][50];
	
	/* getting array of all team IDs */
	char tempID[50];
	char tempName[25];
	int j;
	FILE *teams = fopen(argv[2], "r");
	for (j=0; fscanf(teams, "%s %s",tempID,tempName)==2; j++) {
		strcpy(teamIDs[j], tempID);
	}
	
	FILE *gameResults = fopen(argv[1],"r");
	
	/* cycling games, adding wins and losses */
	while (gameResults != NULL) {
		next_game(gameResults,teamIDs,teamWins,teamLosses,teamTies);
	}
	

	/* printing end result */
	arrayPrint(teams, teamWins, teamLosses, teamTies);
	
	return 0;
}



Any help is much appreciated.
Thanks,
Michael

Is This A Good Question/Topic? 0
  • +

Replies To: Pointer From Integer Without Cast Warning

#2 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: Pointer From Integer Without Cast Warning

Posted 25 October 2010 - 02:44 PM

You are trying to hide the problem with a cast, fix the problem. You are passing a char when it expects an array of arrays of type char.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4223
  • View blog
  • Posts: 13,245
  • Joined: 25-December 09

Re: Pointer From Integer Without Cast Warning

Posted 25 October 2010 - 02:55 PM

    
	/* team IDs from file */
    char team1[50]; 
    char team2[50];

    /* index in array for each team */
    int index1 = getIndex(team1,teamIDs[50][50]);
    int index2 = getIndex(team2,teamIDs[50][50]);




Why do you think your compiler is complaining only about parameter 2 not about parameter 1 ?

What is the difference?

You need to remove the [50[50] from the function calls.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 m_johnst  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-September 10

Re: Pointer From Integer Without Cast Warning

Posted 25 October 2010 - 03:00 PM

View Postjimblumberg, on 25 October 2010 - 01:55 PM, said:

Why do you think your compiler is complaining only about parameter 2 not about parameter 1 ?

What is the difference?

You need to remove the [50[50] from the function calls.

Jim


Thanks, that fixed my problem.
Was This Post Helpful? 0
  • +
  • -

#5 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: Pointer From Integer Without Cast Warning

Posted 26 October 2010 - 12:02 AM

I'd like to clarify my point, as it was entirely undeserving of a down vote.

When you have a variable declared as:
char x[];
The type of x[i] is char.

And with
char x[][]
The type of x[i][j] is char.

That is why I said you passed a char.
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Pointer From Integer Without Cast Warning

Posted 26 October 2010 - 12:29 AM

View Postoscode, on 26 October 2010 - 04:02 PM, said:

I'd like to clarify my point, as it was entirely undeserving of a down vote.


I imagine it wasn't the second sentence that caused the 'not helpful' vote. I am only guessing but I imagine it was this first sentence

View Postoscode, on 26 October 2010 - 06:44 AM, said:

You are trying to hide the problem with a cast, fix the problem.


I'm not at all sure what it means and perhaps the person/s who down voted it were also confused and thought that it was likely to confuse the OP.

Could you cover what that first sentence meant as well. That might address the concerns of whoever thought it was a bad post.

BTW - I did not vote on this thread so I don't have a dog in this fight.
Was This Post Helpful? 0
  • +
  • -

#7 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: Pointer From Integer Without Cast Warning

Posted 26 October 2010 - 12:46 AM

As you certainly know janotte it is possible to hide all kinds of problems with casts. C-style, function-style and reinterpret_casts especially.

A lot of beginners think the compiler is just making noise, or preventing them from doing something which is correct. They have heard of casts and assume they are a silencer. There are many rules on what can and can't be safely cast, so unless you know exactly what you are doing, fix the problem instead of hiding it to have more robust code.

This post has been edited by oscode: 26 October 2010 - 12:47 AM

Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Pointer From Integer Without Cast Warning

Posted 26 October 2010 - 12:56 AM

View Postoscode, on 26 October 2010 - 04:46 PM, said:

As you certainly know janotte it is possible to hide all kinds of problems with casts.


And was the OP doing that?
I can't see it - am I looking straight through it?
What line/s should I be looking at?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1