Solved

Basic Questions I Think

Posted on 2004-04-07
11
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.

623 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