0

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
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
user1185790
  • 623
  • 8
  • 24
  • Not sure if it's a dupe, but it should help you understand the issue: http://stackoverflow.com/questions/434763/are-prototypes-required-for-all-functions-in-c89-c90-or-c99 – Retired Ninja Jan 25 '15 at 00:04
  • `void argc` ??? `void *argv[]` ??? You have to pass compatible arguments to `initializeNumJoeys` and neither `void` is compatible with `int` nor `void **` is compatible with `char **`. – Luis Colorado Jan 27 '15 at 05:36

1 Answers1

3

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.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Ah, yes! The order matters - main should always go on bottom. Thank you iharob. – user1185790 Jan 25 '15 at 00:08
  • Not `main()`, if you invoke any function that is not yet defined, a previous declaration of the function is required by the compiler, so you can have just that, a declaration of the function or "_a function prototype_". – Iharob Al Asimi Jan 25 '15 at 00:10
  • I tried your suggestion (see edit). I no longer receive compiler warnings, but am still receiving the same incorrect number (52). – user1185790 Jan 25 '15 at 00:16
  • @user1185790 Don't edit your question in such a way that invalidates the answer. – Iharob Al Asimi Jan 25 '15 at 00:19
  • My apologies. The latest `strtol` suggest results in the following errors argv_test.c:16: error: parameter 1 (‘argc’) has incomplete type argv_test.c:17: error: conflicting types for ‘initializeNumJoeys’ argv_test.c:8: error: previous declaration of ‘initializeNumJoeys’ was here – user1185790 Jan 25 '15 at 00:25
  • Wow, thank you. So many bugs. Ugh. I wish gcc would be more detailed in explaining the problem. – user1185790 Jan 25 '15 at 00:33
  • 1
    @user1185790 `gcc` can be very detailed just add this options to `gcc` command `gcc -Wall -Wextra -Werror` – Iharob Al Asimi Jan 25 '15 at 00:35
  • The function is implicitly declared as `int initializeNumJoeys()`, and the compiler may assume that it is defined as taking an `int` and a `char **` (and needn't diagnose if it isn't). – mafso Jan 25 '15 at 00:45
  • @mafso I wasn't sure about that part. – Iharob Al Asimi Jan 25 '15 at 00:47
  • Well, who still makes use of implicit declarations? As a suggestion for the answer: it could be pointed out, that C99 (`-std=c99 -pedantic` for GCC) removed implicit declarations, and maybe that a compiler (for any standard) needn't even diagnose the mismatch, as the function declarations aren't in the same scope (of course, a good compiler does warn). – mafso Jan 25 '15 at 00:56