通过指针传递值

时间:2014-10-23 03:45:24

标签: c pointers

29: error: expected declaration specifiers or ‘...’ before ‘*’ token
29: error: conflicting types for ‘kill’
/usr/include/signal.h:126: note: previous declaration of ‘kill’ was here

In function ‘kill’:
32: error: too many arguments to function ‘kill’
36: error: too many arguments to function ‘kill’

我是C的新手,并且难以将值从我的main函数传递到下面的kill函数。此代码旨在在Linux环境中运行。目的是打开一个pid文件,获取pid号,并将pid号传递给要测试的kill函数,如果仍然有效。代码“正在”运行,但该值未被传递给kill,并且仅在将无效参数(错误的pid文件名)传递给if语句时才成功。我试图使用指针传递值,但似乎无法使其工作。任何建议表示赞赏。

TL;博士? 我无法通过指针向函数传递值。

#include <stdio.h>    //Needed for standard I/O
#include <stdlib.h>   //Needed for exit
#include <signal.h>   //Needed for kill function

int main(int argc, char *argv[])
{
    FILE *pf;
    char pidvar;
    char *pidnum;

    pidnum = &pidvar;

    pf = fopen(argv[1], "r");

    if (pf == NULL){
        printf("Pid file doesn't exist\n");
        return 2;
    }
    else {
        do  {*pidnum = getc(pf);         /* get one character from the file */
            }while (*pidnum != EOF);     /* repeat until EOF (end of file) */
            fclose(pf);                 /* Close pidfile */
         }
}

    char *pidnum;

pid_t kill(*pidnum, int sig)         /* function declaration */
{

    if ((kill (*pidnum, 0)) == -1){
         printf("The pid %s is no longer valid", *pidnum);
         return 2;
    }
    else if ((kill (*pidnum, 0)) == 0){
         printf("The pid %s is valid", *pidnum);
         return 0;
    }
}                                                                1,1           Top

1 个答案:

答案 0 :(得分:2)

  

note: previous declaration of ‘kill’ was here

这是编译器告诉你你写的函数叫&#34; kill&#34;与系统提供的功能完全相同。请注意,您的函数也会调用kill,如果没有编译器错误(如果系统函数有不同的名称),您的函数就会自行递归。

为您的功能添加新名称。

相关问题