如何将参数从终端传递给函数 [英] How to pass arguments from terminal to a function

查看:123
本文介绍了如何将参数从终端传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
从命令行将参数传递到C程序中

Possible Duplicate:
Pass arguments into C program from command line

我正在尝试从终端将三个参数传递给一个名为replace的函数.我想知道是否可以从终端进行以下操作

I am trying to pass three arguments from terminal into a function called replace. I would like to know if it is possible to do the following from terminal

  % ./replace d DDD mytest.tx

我在网上看过,但是只能找到直接将值传递给main()而不是内部函数的信息.

I have looked online but can only find information on passing values directly to main() and not the function inside.

我已经编辑了以下主要功能:

I have edited the main functions as following:

void replace(char* string_a, char* string_b, char* string_f)
 {
  }

int main(int argc, char *argv[])
  { 
       if(argc < 4)
  { 
    printf("Not enough arguments\n");
   return 0;
  }

replace(argv[1],argv[2],argv[3]);
 }

推荐答案

您的主要函数应采用以下格式:

Your main function should be in the format:

int main ( int argc, char *argv[] )

argv是指向您的参数的指针.请注意,第一个参数是您的程序的名称.

argv is a pointer to your arguments. Note that the first argument is the name of your program.

这是关于命令行参数的课程:

Here is a lesson on command line arguments:

这篇关于如何将参数从终端传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆