无法将字符串复制到结构中的字符串(C)

发布于 2024-11-02 21:35:12 字数 3579 浏览 2 评论 0原文

我正在尝试使用 strcpy 将一些字符串复制到结构中的字符串。我发布代码:

#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]='\0',
    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]='\0';
    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)='\0';
    printf("%s\n",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)='\0';
    printf("\n%s",integer_bin);
    printf("\n%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("\n%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)='\0';
    printf("\n\n%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("\n\n%s %s %s",fp.sign,fp.exponent,fp.fraction);
}

在 strcpy 部分之前一切正常。 printf("\n\n%s %s %s",符号,integer_bin,fraction_bin); 这个 printf 效果很好。我的字符串的大小等于结构中的字符串。你可以看到我使用 malloc 分配了多少内存。 我没有收到错误,但是当我在主函数中打印值时,它们不正确。 问题可能是什么?

I am trying to copy some strings to strings in a struct using strcpy. I am posting the code:

#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]='\0',
    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]='\0';
    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)='\0';
    printf("%s\n",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)='\0';
    printf("\n%s",integer_bin);
    printf("\n%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("\n%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)='\0';
    printf("\n\n%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("\n\n%s %s %s",fp.sign,fp.exponent,fp.fraction);
}

Everything works well till the strcpy part.
printf("\n\n%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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假情假意假温柔 2024-11-09 21:35:12

您的函数缺少 return 语句。

您应该更加注意编译器警告,或者打开更多警告,或者获得更好的编译器。

Your function is missing its return statement.

You should pay more attention to the compiler warnings, or turn more warnings on, or get a better compiler.

枕花眠 2024-11-09 21:35:12

您错过了函数 stringToFloatingPoint 中的返回...至少这是一个问题...

Add

只是为了让您知道,执行 gcc -std=c99 -pedantic -Wall ./ boh.c -lm 或类似的编译器,会说:

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

Valgrind 会说:

==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);
    }

在这里您正在分配内存,然后您搜索一个字符并将指向已分配内存的指针丢弃...如果您随后尝试释放(pch),则会导致问题...

You missed a return in the function stringToFloatingPoint... at least this one is a problem...

Add

Just to let you know, executing gcc -std=c99 -pedantic -Wall ./boh.c -lm or similar for your compiler, would have say:

./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

You mostly can avoid allocation, especially of two chars or so...

Moreover, running it will give a problem (double free or corruption)...

Likely the leak is here:

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

Here you are allocating memory, then you search for a char and trash the pointer to the allocated memory... If you then will try to free(pch), you will cause a problem...

颜漓半夏 2024-11-09 21:35:12

您的函数被指定为返回 FLOATING_POINT 结构:

FLOATING_POINT stringToFloatingPoint(char* str)

但函数中没有 return 语句。

Your function is specified as returning a FLOATING_POINT structure:

FLOATING_POINT stringToFloatingPoint(char* str)

But nowhere in the function is there a return statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文