c语言返回链表(malloc分配的字符串)

本文介绍了如何在C语言中正确处理返回malloc分配的链表字符串。内容包括通过函数返回头指针、使用二级指针对象进行赋值以及避免常见误区,如一级指针和二级指针的误用,并强调了使用free释放内存以防止内存泄漏的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、函数返回头指针(指向字符串的指针)
以下代码返回了链表的头指针,这种比较简单

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

//对于缺测""则记录特征值999.999
sta_data *readregfile(char *path,char *filename,FILE *log_fp)
{
    sta_data *pHead,*p,*pTail;

    pHead=pTail=(sta_data*)malloc(LEN_DATA);

    char fileallname[512];
    sprintf(fileallname,"%s/%s",path,filename);
    FILE* reg_fp = fopen(fileallname, "r");
    char line[300];
    int num=0;//记录当前读取的num行,前提是每组有四行
    int line_num=0;//记录真实的行数
    int group=0;//记录站数,也就是第几组

    struct station_temp sta_temp;

    while (fgets(line, sizeof(line), reg_fp))
      {
         line_num++;
         if(num-group*4==0)//解析第一行(基本信息)
            {
               if(reglen(line)==34)
                 {
                    int f_status=sscanf(line,"%5s",sta_temp.station_num);
                    strncpy(sta_temp.first,line,34);sta_temp.first[34]='\0';
                    if(f_status!=1)
                       {
                         //重新对错误数据进行异常值赋值,并提醒错误数据
                         sprintf(sta_temp.station_num,"99999");
                         fprintf(log_fp,"%s %d 基本信息字节数正确,格式错误\n",filename,line_num);
                       }
                 }
               else
                  {
                     sprintf(sta_temp.station_num,"99999");
                     strncpy(sta_temp.first,line,34);sta_temp.first[34]='\0';
                     fprintf(log_fp,"%s %d 基本信息行的字节数不正确\n",filename,line_num);
                  }
               num++;
               continue;
            }
         else if(num-group*4==1)//解析第二行(主要要素)
            {
               if(reglen(line)==262)
                 {
                   int s_status=sscanf(line,"%14s %*s %3s %*s %3s %*s %3s %*s %*s %3s \
                   %*s %3s %*s %4s %4s %4s %*s %4s %*s %*s %*s %*s %*s %*s %5s %5s %*s %5s %*s %4s \
                   %4s %*s %4s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %5s %*s %*s %*s", \
                   sta_temp.o_time,sta_temp.wind2,sta_temp.wind10,sta_temp.wind_max,sta_temp.wind_s,sta_temp.wind_j, \
                   sta_temp.rain,sta_temp.tem,sta_temp.tem_max,sta_temp.tem_min,sta_temp.pres,sta_temp.pres_max,sta_temp.pres_min, \
                   sta_temp.g_temp,sta_temp.g_temp_max,sta_temp.g_temp_min,sta_temp.s_pres);
                   strncpy(sta_temp.second,line,262);sta_temp.second[262]='\0';
                   if(s_status!=17)
                       {
                         //重新对错误数据进行异常值赋值,并提醒错误数据
                         sprintf(sta_temp.o_time,"99999999999999");
                         fprintf(log_fp,"%s %d 第二段观测数据字节数正确,格式错误\n",filename,line_num);
                       }
                 }
               else
                  {
                     //重新对错误数据进行异常值赋值,并提醒错误数据
                     sprintf(sta_temp.o_time,"99999999999999");
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值