Link to home
Start Free TrialLog in
Avatar of tolstoyleo
tolstoyleo

asked on

Basic Questions I Think

I am writing a command line program and I need to be able to accept 2 arguments, and I'm just trying to pass them on as strings or arrays to another function.... here goes some code...

#include <stdio.h>
#include <winsock.h>

void doSocketStuff(char[]);

int main(int argc, char *argv[])
{
    if (argc != 2) {
        int a = errorMessage(1);        
    } else {
        doSocketStuff(argv);  // <------- error msg is here
    }        
    return 0;
}

int errorMessage(int em)
{
    if (em == 1) {
        printf("Usage - Source IPADDRESS,PORT\n\n");
    }
    system("PAUSE");
    return 1;
}

void doSocketStuff(char strIP[])
{
    printf("IP ADDRESS = %s\n",strIP[0]);    
    printf("Port = %s\n",strIP[1]);
}






when I make the function call to "doSocketStuff" I get a couple of errors:

like: passing arg 1 of 'dosocketstuff' from incompatible pointer type

I don't understand how i'm supposed to pass it as a string from the argument array basically...

thanks all

-Matt





Avatar of sunnycoder
sunnycoder
Flag of India image

Hi tolstoyleo,

>int main(int argc, char *argv[])
as you notice, argv is an array of char pointers

>void doSocketStuff(char strIP[])
this expects an array of chars ... and you are passing it an array of char *s ....

change the declaration to
void doSocketStuff(char * strIP[]) if you wish to pass it an array of char pointers

Sunnycoder
>I don't understand how i'm supposed to pass it as a string from the argument array basically
If you wanted to pass a single string instead of an array of strings, then modify your function call

>doSocketStuff(argv);
to pass a single string such as argv[1] etc to the function .... here you are passing an array of char *s (each char * holds a string ... so effectively it is an array of strings and not a single string)
Avatar of tolstoyleo
tolstoyleo

ASKER

I changed the function but i'm getting the same error...
tolstoyleo,

Post your modified code so that we could help more ....Also let us know the error message u get on compiling the code

Amit
ok... i'm kind of lost getting back into C... so thanks for bearing in there...

what is wrong with doing this:

#include <stdio.h>

void repeater(char[]);

int main(int argc, char *argv[]) {
    repeater(argv);
    return 0;
}

void repeater(char *arrVar[]) {
    printf("%s\n",arrVar[0]);
}

I get an undefined reference to "repeater" error.. but besides that... when I try playing with just passing the arguments to another function from the command .. it's just not working out for me... i keep getting pointer type errors...
This is the modified code from the first posting of code...

#include <stdio.h>
#include <winsock.h>

//void doSocketStuff(char);

int main(int argc, char *argv[])
{
    if (argc != 2) {
        int a = errorMessage(1);        
    } else {
        doSocketStuff(argv);
    }        
    return 0;
}

int errorMessage(int em)
{
    if (em == 1) {
        printf("Usage - Source IPADDRESS,PORT\n\n");
    }
    system("PAUSE");
    return 1;
}

void doSocketStuff(char *strIP[])
{
    printf("IP ADDRESS = %s\n",strIP[0]);    
    printf("Port = %s\n",strIP[1]);
}


here are the errors:

26 C:\cpp\programs\source\sockets.c
[Warning] type mismatch with previous implicit declaration

11 C:\cpp\programs\source\sockets.c
[Warning] previous implicit declaration of `doSocketStuff'


26 C:\cpp\programs\source\sockets.c
[Warning] `doSocketStuff' was previously implicitly declared to return `int'

ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#include <stdio.h>

void repeater(char[]);

int main(int argc, char *argv[]) {
    repeater(argv);
    return 0;
}

void repeater(char *arrVar[]) {
    printf("%s\n",arrVar[0]);
}



Initially, the function prototype for 'repeater' tells the compiler that u have a function called 'repeater' which takes a char array (pointer) as a parameter and returns void

When in main, u call, repeater ( argv ), u are passing an array of char pointers [can say an array of strings] to the function repeater BUT the function prototype u declared earlier expects just a char string & not an array of char strings

Thus, u get an undefied reference

Amit
problem is that by default C expects a function to return an int while your function is returning void ... thus the mismatch
I'm going to accept the last 2 of sunny coders and award the pointers... one more question but you don't have to answer if you don't want to:

why is the first value in the argv array = the location of the program like:

argv[0] = "C:\cpp\programs\source\sockets.exe

and the second one:

argv[1] = whatever the arguments i typed:  3.3.3.3 , 80
well that is the way C works ... argv[0] is always the name of the program which is invoked and the following paramteres are arguments to the program ...
Why is it so ? Well I can't recollect where but I saw a couple of programs which saved the name of their executables for restart or some other procesing