LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c warnings (https://www.linuxquestions.org/questions/programming-9/c-warnings-477206/)

kpachopoulos 08-25-2006 05:03 AM

c warnings
 
Hi,
these warnings correspond to the following functions. Unfortunately, i can't fix them and get a "segmentation fault". Can somebody help?

Code:

kostas@vakhos:~/c_programming$ gcc -o res res.c
res.c: In function ‘get_cmdline_name’:
res.c:213: warning: return makes pointer from integer without a cast
res.c: In function ‘get_stat_name’:
res.c:234: warning: comparison between pointer and integer
res.c:237: warning: assignment makes integer from pointer without a cast
res.c:237: warning: assignment makes integer from pointer without a cast
res.c:241: warning: assignment makes integer from pointer without a cast
res.c:241: warning: return makes pointer from integer without a cast

Code:


...
char full_path[80];
...
char get_cmdline_name(int proc_id)
{       
        char cmd[360];
        char proc_id_str[64];       

        initialize_buffer(cmd);
        initialize_buffer(full_path);
        initialize_buffer(proc_id_str);

        FILE* f;
        sprintf(proc_id_str, "%d", proc_id);

        strcat(full_path, "/proc/");       
        strcat(full_path, proc_id_str);
        strcat(full_path, "/cmdline");       
       

        if ((f=fopen(full_path,"r"))!=NULL)
        {
                fscanf(f,"%s", cmd);
        }

        return cmd;
}

char get_stat_name(int proc_id)
{       
        char cmd[360];
        char proc_id_str[64];

        initialize_buffer(cmd);
        initialize_buffer(full_path);
        initialize_buffer(proc_id_str);

        FILE* f;
        sprintf(proc_id_str, "%d", proc_id);
       
        strcat(full_path, "/proc/");
        strcat(full_path, proc_id_str);
        strcat(full_path, "/stat");       
       
        char ch='\0';
        if ((f=fopen(full_path,"r"))!=NULL)
        {
                while (ch!='\n')
                {
                        ch= (char)fgetc(f);
                        if ((ch='(') && (ch=')'))
                        {
                                ch= (char)fgetc(f);
                                strcat(cmd, &ch);
                                if (ch=")")
                                        return cmd;
                        }
                }
        }

        return "\0";
}


dmail 08-25-2006 06:45 AM

Quote:

res.c: In function ‘get_cmdline_name’:
res.c:213: warning: return makes pointer from integer without a cast
Code:

char get_cmdline_name(int proc_id)
{
char cmd[360];
...
return cmd;
}

The name of an array is a pointer to the first element. As shown above in get_cmdline_name you are returning a char pointer, this can be fixed by changing the return type to "char*".


In get_stat_name you a have similar problem although this time you have two return paths one which returns a char pointer and one which returns a char(eof which shows a failure). You may fix this using the same mathod as the first but on failure return a 0/NULL.
Also in get_stat_name you have a typo
Quote:

if (ch=")")
should read
Quote:

if (ch==')')
and a logic and typo error above that
Quote:

if ((ch='(') && (ch=')'))
You are checking one char and wanting it to be two values?Well actually you are assigning the values(thats the typo).


All times are GMT -5. The time now is 07:28 PM.