求助:复制串中的乱码问题
程序代码:
#include<stdio.h> #include<stdlib.h> typedef struct { char *data; int length; }SqString; int StrAssign(SqString *Sq,char *ch); void Pri_String(SqString *str); int main() { SqString str,*sq; sq=&str; printf("%o\n",&str); printf("%o\n",sq); StrAssign(sq,"Hello!"); Pri_String(sq); return 0; } int StrAssign(SqString *Sq,char *ch) /*由串常量ch创建串sq 1.遍历ch求其长度,对应分配Sq空间 2.复制,Sq长度*/ { printf("1\n"); char *flag=ch; int len; while(*flag++!='\0') continue; len=flag-ch; Sq->data=(char *)malloc((len+1)*sizeof(char)); printf("2\n"); if(Sq->data==NULL) { printf("Apply space is not succesed!"); exit(0); } while(*(Sq->data)++=*ch++) ; Sq->length=len; printf("3\n"); return 0; } void Pri_String(SqString *str) { for(int i=0;i<str->length;i++) putchar(*str->data++); printf("\n"); printf("%s\n",str->data); printf("%d\n",str->length); }