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

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

#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");
                
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将一个链表转化为字符串,可以按照以下步骤进行: 1. 定义一个字符数组,用于存储转化后的字符串; 2. 遍历链表,将每个节点的值转化为字符,并拼接到字符数组中; 3. 在字符数组末尾添加一个结束符 '\0',表示字符串的结束。 下面是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> struct ListNode { int val; struct ListNode *next; }; char* listToString(struct ListNode* head) { // 计算链表长度 int len = 0; struct ListNode* p = head; while (p != NULL) { len++; p = p->next; } // 分配字符数组空间 char* str = (char*)malloc(sizeof(char) * (len * 2 + 1)); if (str == NULL) { printf("Memory allocation failed.\n"); return NULL; } // 遍历链表,将每个节点的值转化为字符 p = head; int i = 0; while (p != NULL) { str[i++] = p->val + '0'; str[i++] = '-'; p = p->next; } // 在字符数组末尾添加一个结束符 str[i] = '\0'; return str; } int main() { // 创建一个链表 struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode)); head->val = 1; head->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->val = 2; head->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->next->val = 3; head->next->next->next = NULL; // 将链表转化为字符串 char* str = listToString(head); printf("The string representation of the list is: %s\n", str); // 释放链表和字符数组的内存 struct ListNode* p = head; while (p != NULL) { struct ListNode* q = p->next; free(p); p = q; } free(str); return 0; } ``` 该代码中,listToString 函数接受一个链表的头节点指针作为参数,返回一个指向字符数组的指针。程序运行结果如下: ``` The string representation of the list is: 1-2-3- ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值