Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

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





0
tolstoyleo
Asked:
tolstoyleo
  • 5
  • 4
  • 2
1 Solution
 
sunnycoderCommented:
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
0
 
sunnycoderCommented:
>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)
0
 
tolstoyleoAuthor Commented:
I changed the function but i'm getting the same error...
0
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
Sys_ProgCommented:
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
0
 
tolstoyleoAuthor Commented:
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...
0
 
tolstoyleoAuthor Commented:
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'

0
 
sunnycoderCommented:
>//void doSocketStuff(char);
change declaration to

void doSocketStuff(char * a[] );
0
 
Sys_ProgCommented:
#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
0
 
sunnycoderCommented:
problem is that by default C expects a function to return an int while your function is returning void ... thus the mismatch
0
 
tolstoyleoAuthor Commented:
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
0
 
sunnycoderCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now