Solved

Basic Questions I Think

Posted on 2004-04-07
11
262 Views
Last Modified: 2010-04-15
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
Comment
Question by:tolstoyleo
  • 5
  • 4
  • 2
11 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10781068
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10781085
>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
 
LVL 1

Author Comment

by:tolstoyleo
ID: 10781095
I changed the function but i'm getting the same error...
0
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10781119
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
 
LVL 1

Author Comment

by:tolstoyleo
ID: 10781128
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
 
LVL 1

Author Comment

by:tolstoyleo
ID: 10781137
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 10781166
>//void doSocketStuff(char);
change declaration to

void doSocketStuff(char * a[] );
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10781171
#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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10781177
problem is that by default C expects a function to return an int while your function is returning void ... thus the mismatch
0
 
LVL 1

Author Comment

by:tolstoyleo
ID: 10781204
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10781220
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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question