貌似把scanf ("%[^\n]", chars);换成gets(chars);就可以,可是为什么?
#include <stdio.h>
#include <stdlib.h>
#include
using namespace std;
typedef struct SString {
char* ch;
int length;
}SString;
void SStringInit (SString* S)
{
S->ch = NULL;
S->length = 0;
}
void SStringAssign (SString* S)
{
S->ch = (char*)malloc(256sizeof(char));
char chars[256];
scanf ("%[^\n]", chars);
int len = 0;
char c = chars;
while (*c != '\0')
{
len ++;
c ++;
}
for (int i = 1; i <= len; i++)
S->ch[i] = chars[i-1];
S->length = len;
}
void SStringPrint (SString* S)
{
for (int i = 1;i <= S->length; i++)
printf ("%c", S->ch[i]);
printf ("\n");
}
int main ()
{
SString str1, t;
SStringInit (&str1);
SStringAssign (&str1);
SStringPrint (&str1);
SStringInit (&t);
SStringAssign (&t);
SStringPrint (&t);
return 0;
}