English 中文(简体)
硬拷贝
原标题:Cant copy a string to a string in a struct ( C )

我正试图复制一些插手,用地块铺设。 我在张贴该法典:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct floating_point{
    char sign[2];
    char exponent[9];
    char fraction[24];
}FLOATING_POINT;
FLOATING_POINT stringToFloatingPoint(char* str){
    struct floating_point fp;
    char *integer_str,*fraction_str,*integer_bin,*fraction_bin,*sign;
    sign=(char*)malloc(2*sizeof(char));
    int i=1,integer=0,fraction=0,comma=0,remainder=0,size=0;
    double fraction_double;
    //str=(char*)malloc(200*sizeof(char));
    //str="108,53";
    char * pch=(char*)malloc((strlen(str)+1)*sizeof(char));
    pch=strchr(str, , );
    if(pch==NULL){
        pch=str+strlen(str);
    }
    comma=pch-str;
    integer_str=(char*)malloc((comma+1)*sizeof(char));
    strncpy(integer_str,str,comma);
    integer_str[comma]=  ,
    integer=atoi(integer_str);
    fraction_str=(char*)malloc((strlen(str)-comma+2)*sizeof(char));
    strncpy(fraction_str,str+comma+1,strlen(str)-1);
    fraction_str[strlen(str)-comma]=  ;
    fraction=atoi(fraction_str);
    printf("%d",fraction);
    printf("%s",fraction_str);
    if(integer<0){
        strcpy(sign,"1");
        integer=-1*integer;
    }
    else{
        strcpy(sign,"0");
    }
    size=(int)(log(integer)/log(2))+2;
    integer_bin=(char*)malloc(size*sizeof(char));
    while(integer>0){
        remainder=fmod(integer,2);
        integer=integer/2;
        *(integer_bin+size-i-1)=(char)(remainder+48);
        i++;
    }
    *(integer_bin+size-i-1)=(char)(integer+48);
    *(integer_bin+size-1)=  ;
    printf("%s
",integer_bin);
    fraction_bin=(char*)malloc(24*sizeof(char));
    fraction_double=atof(fraction_str);
    fraction_double=fraction_double/(pow(10,strlen(fraction_str)));
    printf("frac= %f",fraction_double);
    i=0;
    while((i<23)&&fraction_double!=0){
        fraction_double=2*fraction_double;
        if(fraction_double<1){
            *(fraction_bin+i)= 0 ;
        }
        else{
            fraction_double=fraction_double-1;
            *(fraction_bin+i)= 1 ;
        }
        i++;
    }
    *(fraction_bin+i)=  ;
    printf("
%s",integer_bin);
    printf("
%s",fraction_bin);
    size=strlen(integer_bin);
    for(i=0;i<strlen(fraction_bin)-(size-1);i++){
        *(fraction_bin+strlen(fraction_bin)-1-i)=*(fraction_bin+strlen(fraction_bin)-size-i);
    }
    for(i=1;i<size;i++){
        *(fraction_bin+i-1)=*(integer_bin+i);
    }
    printf("
%s",fraction_bin);
    free(integer_bin);
    integer_bin=(char*)malloc(9*sizeof(char));
    strcpy(integer_bin,"00000000");
    printf("integer_bin %s",integer_bin);
    integer=127+size-1;
    i=1;
    while(integer>0){
        remainder=fmod(integer,2);
        integer=integer/2;
        *(integer_bin+8-i)=(char)(remainder+48);
        i++;

    }

    *(integer_bin+8-i)=(char)(integer+48);
    *(integer_bin+8)=  ;
    printf("

%s %s %s",sign,integer_bin,fraction_bin);
    strcpy(fp.exponent,integer_bin);
    strcpy(fp.sign,sign);
    strcpy(fp.fraction,fraction_bin);
    free(integer_bin);
    free(integer_str);
    free(fraction_bin);
    free(fraction_str);
    free(sign);
    free(pch);
}
int main()
{

    struct floating_point fp=stringToFloatingPoint("108,53");
    printf("

%s %s %s",fp.sign,fp.exponent,fp.fraction);
}

Everything works well till the strcpy part. printf(" %s %s %s",sign,integer_bin,fraction_bin); This printf works well. And the size of my strings are equal to the ones in the struct. You can see how much memory I have allocated using malloc. I dont get an error but when I print the values in my main funciton they are not correct. What can the problem be?

最佳回答

页: 1

你们应当更多地注意汇编者发出的警告,或对汇编者发出更多的警告,或获得更好的汇编者。

问题回答

你错过了“ToFloatingPoint”功能的回报......至少这是一个问题。

仅请您知道,执行gcc-std=c99 -pedantic - /boh.c -lm 或与您的汇编者类似:

./boh.c: In function ‘stringToFloatingPoint’:
./boh.c:105: warning: control reaches end of non-void function

Valgrind would say:

==6480== HEAP SUMMARY:
==6480==     in use at exit: 7 bytes in 1 blocks
==6480==   total heap usage: 7 allocs, 7 frees, 59 bytes allocated
==6480== 
==6480== LEAK SUMMARY:
==6480==    definitely lost: 7 bytes in 1 blocks

你们大多可以避免分配,特别是两个分支。

此外,管理它会造成问题(双重自由或腐败)。

与此类似,泄漏发生在这里:

   char * pch=(char*)malloc((strlen(str)+1)*sizeof(char));
    pch=strchr(str, , );
    if(pch==NULL){
        pch=str+strlen(str);
    }

这里,你正在分配记忆,然后,你寻找一个char子,把点子放在所分配的记忆中...... 如果届时你将设法释放(pch),你将造成问题......

你的职能被指定为返回联络处。 POINT结构:

FLOAMENT_POINT string ToFloating Point (char* str)

但这一职能中没有任何地方有返回声明。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签