함수를 찾고 C 파일에서 함수의 행 수를 계산하기 위해 C 프로그램을 작성하고 구조에 저장하고 있습니다. 아래 코드를 입력했습니다.C로 세부 사항 저장하기
#include <stdio.h>
#include <string.h>
#define SIZE 1024
struct fundetails
{
int nooflines;
char *funcname;
}s[20];
char *ffname(char *line)
{
int i=1,j=0;
char *dt;
char name[SIZE];
strtok(line,"(");
dt = strchr(line,' ');
if(dt[i] == '*')
i++;
while(dt[i] != '\0')
{
name[j]=dt[i];
i++;
j++;
}
name[j] ='\0';
return name;
}
int main(int argc, char **argv)
{
if(argc < 2)
{
printf("Give the filename \n");
printf("Usage: %s filename\n", argv[0]);
return -1;
}
int i, lines =0, funlines =0,count =0, fn =0, flag =0, size=0,emptyflag=0;
char c[SIZE],b[SIZE];
char *fname;
FILE *fd;
fd = fopen(argv[1],"r");
while(fgets(c,SIZE,fd))
{
emptyflag=0;
lines++;
size = strlen(c);
if(size == 1 && (strcmp(c,"\n"))== 0)
emptyflag=1;
for(i=0;i<size;i++)
{
while(c[i] =='\t' || c[i] == ' ')
{
i++;
}
if(c[i] == '{')
{
count++;
if(flag)
{
if(!emptyflag)
funlines++;
else
emptyflag=0;
}
if(count == 1)
{
fn++;
printf("Function %d is Started..............\n", fn);
flag = 1;
fname=ffname(b);
printf("Function name is:%s\n",fname);
}
break;
}
else if(c[i] == '}')
{
count--;
if(!count)
{
flag = 0;
printf("No of lines in the function %d is: %d\n", fn, funlines);
printf("Function %d is finished..........\n", fn);
s[fn-1].nooflines=funlines;
s[fn-1].funcname=fname;
funlines = 0;
}
else
{
if(!emptyflag)
funlines++;
else
emptyflag=0;
}
break;
}
else if(flag)
{
if(!emptyflag)
funlines++;
else
emptyflag=0;
break;
}
}
strcpy(b,c);
}
printf("FUN_NAME\tNO_OF_LINES\n");
for(i=0;i<fn;i++)
{
printf("%s\t\t%d\n",s[i].funcname,s[i].nooflines);
}
return 0;
}
경고가 try.c:26:2: warning: function returns address of local variable [enabled by default]
으로 생성됩니다. 그리고 아래와 같이 결과를 산출합니다. I 라인 위
s[fn-1].nooflines=funlines;
s[fn-1].funcname=fname;
의 때마다, GDB
에서 실행 동일한 loop.WQhile의 i1m에 function name and no of lines
를 기억하고있어
Function 1 is Started..............
Function name is:fundetails
No of lines in the function 1 is: 2
Function 1 is finished..........
Function 2 is Started..............
Function name is:dhahira
No of lines in the function 2 is: 1
Function 2 is finished..........
Function 3 is Started..............
Function name is:add
No of lines in the function 3 is: 3
Function 3 is finished..........
Function 4 is Started..............
Function name is:sub
No of lines in the function 4 is: 9
Function 4 is finished..........
Function 5 is Started..............
Function name is:main
No of lines in the function 5 is: 13
Function 5 is finished..........
FUN_NAME NO_OF_LINES
main 2
main 1
main 3
main 9
main 13
, 라인의 수는 구성하지 correctly.But 저장해 함수 이름의 경우. Problem:
줄에 제대로 작동하고 함수 이름에 대해 작동하지 않는 이유가 무엇인지 이해가 안됩니까? 그 경고 때문인가? 고마워. 고마워.
name []을 전역으로 선언하면 will 이 문제가 해결되었습니다. – Dhasneem
name []을 global로 정의하면 progream의 수명주기를 통해 메모리가 점유됩니다. 문제를 해결할 것입니다.그러나 그것은 최선의 해결책이 아닙니다. 더 좋은 방법은 name을 ffname()에 저장하기위한 버퍼를 전달하는 것입니다. – TieDad
감사합니다. 경고를 극복하는 것이 좋습니다. 그러나 출력은 동일하게 유지됩니다. – Dhasneem