Reputation: 703
I'm trying to populate a global int variable by passing command line arguments to a function. When I do this, I get warnings (see below), as well as a funky return number (such as 52 instead of the expected 49).
Any hints would be greatly appreciated. This is HW - but only a very small portion of the overall assignment.
#include <stdio.h>
#include <stdlib.h>
#include "kangarooHeaders.h"
int numJoeys = MIN_NUM_LEGAL_JOEYS - 1;
int main (int argc, char* argv[])
{
initializeNumJoeys(argc,argv);
printf("%d", numJoeys);
}
void initializeNumJoeys(void argc, void *argv[])
{
char line[LINE_LEN];
if (argc > MAMAS_NUM_JOEYS_CMD_LINE_INDEX)
numJoeys = *argv[1];
}
argv_test.c:13: warning: conflicting types for ‘initializeNumJoeys’
argv_test.c:9: warning: previous implicit declaration of ‘initializeNumJoeys’ was here
Upvotes: 0
Views: 1137
Reputation: 53046
Put this above the main()
function
void initializeNumJoeys(int argc, char *argv[]);
the reason is implicit function declaration, the compiler doesn't find a prototype for initializeNumJoeys()
and implicitly declares it as
int initializeNumJoeys();
so when it finds the definition, then it's conflicting with the previous declaration.
Also, change this
numJoeys = *argv[1];
to
numJoeys = strtol(argv[1], NULL, 10);
and also, the function signature is wrong
void initializeNumJoeys(void argc, void *argv[])
/* ^ should be int */
so change it to
void initializeNumJoeys(int argc, void *argv[])
don't forget to fix the prototype.
Upvotes: 3