typedef struct Email
{
char domain [128];
char local [128];
} Email ;
Email email_in(char s[])
{
Email *result ;
int c=0;
for(c=0;c<128;c++)
{
result->domain[c]='\0';
result->local[c]='\0';
}
char *ss = s;//PG_GETARG_CSTRING(0);
//result = 0;
int i,j, ind1=0, x=0 , dot=0;
int length= strlen(ss);
for(i=0; i<length;i++)
{
if(ss[i]<='Z' && ss[i]>='A' )
{
ss[i]=ss[i]+32;
}
}
;
if ( ((ss[0]<'a') || (ss[0]>'z')) )
{
if((ss[0]<'A') || (ss[0]>'Z'))
perror("Error printed by perror=1");
}
else
{
for ( i=0;i<length;i++)
{
if (ss[i]=='@')
{
result->local[i]=ss[i];
ind1=i+1;
break;
}
else
if(ss[i]=='-' || ss[i]=='.')
if(ss[i-1]=='-' || ss[i-1]=='.')
{
perror("Error printed by perror=4 ");
}
if ( ((ss[i]<'a') || (ss[i]>'z') )&&((ss[i]<'0')||(ss[i]>'9')) )
{
if(((ss[i]<'A') || (ss[i]>'Z')) &&(ss[i]!='-') &&(ss[i]!='.') )
{
printf("%c \n",ss[i]);
perror("Error printed by perror=2");
}
}
if(ss[i]!='@')
result->local [i]=ss[i];
}
for ( j=ind1;j<length;j++)
{
if(ss[j]=='.')
dot= dot+1;
if(ss[j]=='-' || ss[j]=='.')
if(ss[j-1]=='-' || ss[j-1]=='.')
{
perror("Error printed by perror=6 ");
}
if ( (ss[j]<'a') || ((ss[j]>'z' ) ) )
{
if(((ss[j]<'A') || (ss[j]>'Z')) &&(ss[j]!='-') &&(ss[j]!='.') )
perror("Error printed by perror=3 ");
}
result->domain [x]=ss[j];
x=x+1;
}
result->domain[x+1]='\0';
result->domain[x]='\0';
if(dot<1 && dot >2)
perror("invalid domain");
}
return*(result);//PG_RETURN_POINTER(result);
}
void main()
{
char s1[]="tami@openu.ac.il\0";
email_in(s1);
//printf("%s",s1);
}
当它到达result->domain[c]='\0';
时,它显示我错误
"reslut=null"
我需要指针来改变原始副本但是如何做到这一点?
此函数用于验证PostgreSQL
中用于添加新数据类型的电子邮件地址
我对c或c ++没有经验 我需要帮助&gt;&lt;
答案 0 :(得分:2)
当你这样做时
Email *result ;
你得到一个指向电子邮件的指针,但你没有得到电子邮件变量。
在这种情况下,您应该避免指针并直接使用结构。类似的东西:
#include <stdio.h>
#include <string.h>
typedef struct Email
{
char domain [128];
char local [128];
} Email ;
Email email_in()
{
Email result; // Don't use pointer
strcpy(result.domain, "test_domain");
strcpy(result.local, "test_local");
return result;
}
int main(void) {
Email x=email_in(); // Make the variable x of type Email
printf("%s\n", x.domain);
printf("%s\n", x.local);
return 0;
}
如果您更喜欢使用指针,可以尝试以下方法:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Email
{
char domain [128];
char local [128];
} Email ;
Email* email_in()
{
Email* result;
result = malloc(sizeof(Email)); // Allocate memory for an Email and make result point to it
strcpy(result->domain, "test_domain");
strcpy(result->local, "test_local");
return result;
}
int main(void) {
Email* x=email_in(); // Make the variable x of type pointer to Email
printf("%s\n", x->domain);
printf("%s\n", x->local);
free(x); // Free the allocated memory
return 0;
}