带数组的c函数不返回值

带数组的c函数不返回值,c,arrays,malloc,C,Arrays,Malloc,我写了下面的c代码 #include <stdio.h> #include <stdlib.h> char *read_from_file() { FILE *ptr_file; char buf[22]; char buf_old[sizeof(buf)]; char candata_needed[4]; ptr_file =fopen("/home/pi/probe.txt","r"); if (!

我写了下面的c代码

#include <stdio.h>
#include <stdlib.h>

char *read_from_file()
{

    FILE *ptr_file;
    char buf[22];
    char buf_old[sizeof(buf)];
    char candata_needed[4]; 
        ptr_file =fopen("/home/pi/probe.txt","r");
        if (!ptr_file){
            fclose(ptr_file);
            return 1;
        }
        if (fgets(buf,sizeof(buf), ptr_file)!=NULL){

            if(memcmp(buf, buf_old, sizeof(buf)))
            {
                memcpy(candata_needed, buf + 17, sizeof(buf));              
                memcpy(buf_old, buf, sizeof(buf));
            }
        }       
        fclose(ptr_file);    
   return (candata_needed);        
}    

int main()
{
        char *candata;
        int i;

    while(1){
        printf("stape 1\n");        
        candata=read_from_file()
        for(i=0; i<4; i++)
        {
            candata[i] = *(candata + i);
        }
        printf("candata: %s\n", candata); 
    }
        free(candata);
        return 0;
}
#包括
#包括
char*从_文件()读取_
{
文件*ptr_文件;
char-buf[22];
char buf_old[sizeof(buf)];
char candata_需要[4];
ptr_file=fopen(“/home/pi/probe.txt”,“r”);
如果(!ptr_文件){
fclose(ptr_文件);
返回1;
}
if(fgets(buf,sizeof(buf),ptr_文件)!=NULL){
if(memcmp(buf,buf_old,sizeof(buf)))
{
memcpy(需要坎达塔,buf+17,sizeof(buf));
memcpy(buf_old,buf,sizeof(buf));
}
}       
fclose(ptr_文件);
返回(所需数据);
}    
int main()
{
char*candata;
int i;
而(1){
printf(“stape 1\n”);
candata=从文件()中读取

对于(i=0;i您正在返回一个指向本地数组的指针。一旦函数返回,该本地数组将超出范围,留下一个指向不再存在的数组的指针。尝试使用此指针将导致未定义的行为

有几种解决方案:

  • 使数组成为全局变量

  • 使数组
    为静态
    。这意味着它的生存期将延长到程序的生存期,并且返回后指向它的指针仍然有效

  • 使用
    malloc
    动态分配,并返回该指针。使用完内存后,不要忘记释放内存

  • 在调用函数中创建数组,并将指向该数组的指针作为参数传递。让函数填充该数组。如果函数不知道大小(可能的元素数),则也需要将其作为参数传递


我个人推荐最后一种解决方案,但“最好”可能会因用例的不同而有所不同。我确实不推荐第一种解决方案,将其设置为全局变量。我也不喜欢第二种解决方案,将变量设置为静态变量,这与在IMO中设置全局变量没有什么区别。

所需的candata\u
具有自动存储持续时间。它会自动运行当函数结束时,作用域的t。所以不要试图返回指向它的指针

使用返回指针的行为未定义


一种解决方案是传递一个字符缓冲区及其大小,以
从_文件读取
@Joachim Pileborg:我已经更改了代码,现在正在使用
malloc

#include <stdio.h>
#include <stdlib.h>

void read_from_file(char *result, int lenght_result)
{

    FILE *ptr_file;
    char buf[22];
    char buf_old[sizeof(buf)];
    char candata_needed[4]; 
    //while(1){
        ptr_file =fopen("/home/pi/probe.txt","r");
        if (!ptr_file){
            return 1;
        }
        if (fgets(buf,sizeof(buf), ptr_file)!=NULL){

            if(memcmp(buf, buf_old, sizeof(buf)))
            {

                memcpy(candata_needed, buf + 17, sizeof(buf));
                strncpy(result, candata_needed, lenght_result);

                memcpy(buf_old, buf, sizeof(buf));
            }
        }

        fclose(ptr_file); 
}


int main()
{
        int i;
        int size=4;
        char *candata = malloc(size);           
    while(1){

        read_from_file(candata, size);

        printf("candata: %s\n", candata); 

    }
    free(candata);        
    return 0; 
#包括
#包括
无效从文件读取(字符*结果,整数长度结果)
{
文件*ptr_文件;
char-buf[22];
char buf_old[sizeof(buf)];
char candata_需要[4];
//而(1){
ptr_file=fopen(“/home/pi/probe.txt”,“r”);
如果(!ptr_文件){
返回1;
}
if(fgets(buf,sizeof(buf),ptr_文件)!=NULL){
if(memcmp(buf,buf_old,sizeof(buf)))
{
memcpy(需要坎达塔,buf+17,sizeof(buf));
strncpy(结果、需要的数据、长度结果);
memcpy(buf_old,buf,sizeof(buf));
}
}
fclose(ptr_文件);
}
int main()
{
int i;
int size=4;
char*candata=malloc(大小);
而(1){
从_文件读取_(candata,大小);
printf(“candata:%s\n”,candata);
}
免费(坎达);
返回0;
代码可以工作,我可以得到数据。这样可以吗?还是不推荐? 我的最终目标是将数据作为输出发送到另一个c程序。
我是C语言新手,很难找到解决此问题的最佳方法。

如果(!ptr_file)fclose(ptr_file);
没有意义。如果文件未打开,则无需关闭。事实上,您正在调用
fclose(0)
在这种情况下,这当然没有什么用处!有趣的是,你应该使用malloc或类似的机制,并且你标记,但你不使用它!注意:
return
不是一个函数,而是一个语句。不要在简单的参数周围加括号,这可能会导致拼写错误。投票结果:我更喜欢fi一个国家英里的最终解决方案。谢谢。我会与malloc再次尝试,这是“最好的”处理函数中数组的方法。我无法抗拒,请原谅我:这种方法的一个问题是,如果此函数最终出现在库中,那么该库的
malloc
可能会使用与您的
free
不同的C运行时库。看着您的计算机慢慢融化。