gpt4 book ai didi

c - 当我尝试在 c 中初始化时的指针

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:50 25 4
gpt4 key购买 nike

  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++ 零经验我需要帮助><

最佳答案

当你做的时候

Email *result ;

您得到一个指向 Email 的指针,但没有得到 Email 变量。

在这种情况下,您应该避免使用指针并直接使用结构。像这样的东西:

#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;
}

关于c - 当我尝试在 c 中初始化时的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36417086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com