char指针不能打印正确的值

时间:2014-11-11 09:49:56

标签: c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "full_regr.h"

int full_regr()
{
    struct commands *s1=NULL;      
    s1 = (struct commands *)malloc(sizeof(struct commands));        
    Char *token1;
    Char *token2;        
    char *char_frequency_cat;   
    char *frequency_array[100];

    for(j=0;j<=24;j++)
    {   
        token1=strtok(s1->frequency[j],".");  
        token2=strtok(NULL,".");            //rhs of frequency
        if(!token2) //If decimal not placed in frequency
        {
            printf("\t\tFrequencySSM......:==== %s\n",s1->frequency[1]);**     /// it will print 1025
            token1 = strcat(s1->frequency[j],"000000");
            printf("\t\tFrequencySSM......:==== %s\n",s1->frequency[1]);     // it will print 00000
            char_frequency_cat= token1;
            printf("\t\tin if char_freq: %s\n",char_frequency_cat);
        }            

    }
}        

输出是:     S1-&GT;频率[0] = 1012     s1-&gt;频率[1] = 0000 ///值应为1025

我正在尝试从input.txt文件中读取频率值。我们在频率中使用的格式为FREQ 1012,1025; 整个代码处于for循环中,因此每个频率可以逐个读出并用于进一步处理,因此问题是第一个频率即1012它正在工作但是strcat(s1->frequency[j])第二个频率变为零,如果我增加,即第三频率,我什么也得不到。 char *frequency[1000]在结构中定义

1 个答案:

答案 0 :(得分:1)

s1指向malloc分配的内存。这是未初始化的。

使用strtok解析未初始化的内存会产生未定义的行为。如果你想读取文件“input.txt”,你应该打开并阅读它。