Solved

Basic Questions I Think

Posted on 2004-04-07
11
235 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
Comment Utility
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
Comment Utility
>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
Comment Utility
I changed the function but i'm getting the same error...
0
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:tolstoyleo
Comment Utility
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
Comment Utility
>//void doSocketStuff(char);
change declaration to

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

Expert Comment

by:Sys_Prog
Comment Utility
#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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now