C++ 有人能解释一下这种魔力吗?(scanf,结构,指针)c++;

C++ 有人能解释一下这种魔力吗?(scanf,结构,指针)c++;,c++,C++,我真的不明白这是与scanf或printf有关还是与->,&???有关??? 有人能解释为什么会这样吗?是因为scanf吗?还是因为我没有分配内存?或者wtf?您扫描到一个内存位置,您打印一个值并告诉它它是什么类型的值(例如%s表示char*,%d表示int,%c表示'char,等等),更像: struct persoana{ char *numePrenume; char *strada, *oras, *judet; int varsta; }; void citir

我真的不明白这是与scanf或printf有关还是与->,&???有关???
有人能解释为什么会这样吗?是因为scanf吗?还是因为我没有分配内存?或者wtf?

扫描
到一个内存位置,您
打印一个值并告诉它它是什么类型的值(例如
%s
表示
char*
%d
表示
int
%c
表示'char,等等),更像:

struct persoana{
    char *numePrenume;
    char *strada, *oras, *judet;
    int varsta;
};
void citire(struct persoana *p)
{

    printf("NUME si PRENUME:");
    scanf(" %[^\n]s", &p->numePrenume);
    printf("STRADA:");
    scanf(" %[^\n]s", &p->strada);
    printf("JUDET:");
    scanf(" %[^\n]s", &p->judet);
    printf("ORAS:");
    scanf(" %[^\n]s", &p->oras);
    printf("VARSTA:");
    scanf(" %[^\n]s", &p->varsta);

}
void init(struct persoana *p)
{
    p->numePrenume = "ham";
    p->strada = "ham";
    p->judet = "ham";
    p->oras = "ham";
    p->varsta = 1;
}
void afis(struct persoana *p)
{
    printf("%s\n", &p->numePrenume);
    printf("%s\n", &(p->strada));
    printf("%s\n", &(p->judet));
    printf("%s\n", &(p->oras));
    printf("%s\n", &(p->varsta));
}

代码从不为
persona
中指针指向的字段分配内存。对任何这些字段调用
scanf
的程序具有未定义的行为。对其中一个对象调用
init
,没有帮助,因为它为这些字段分配只读字符串数组;完成后调用
scanf
,也会产生未定义的行为。

下面是代码的最小修改版本,可以正常工作。这不是最好的解决方案,更像是在C中完成的。有一些注释解释了更改

void afis(struct persoana *p)
{
    printf("%s\n", p->numePrenume);
    printf("%s\n", p->strada);
    printf("%s\n", p->judet);
    printf("%s\n", p->oras);
    printf("%d\n", p->varsta);
}
#包括
#包括
persoana结构{
//将成员声明为字符数组(注意固定大小)
//如果声明为指针,则必须对其进行malloc'ated,可能在init函数中。
char numePrenume[50];
char strada[50]、oras[50]、judet[50];
瓦斯塔国际酒店;
};
无效花旗银行(结构人*p)
{
//对于数组或指针类型的变量,它们已经表示
//不应使用如此“&”的地址
printf(“NUME si PRENUME:”);
scanf(“%[^\n]s”,p->numePrenume);
printf(“斯特拉达:”);
scanf(“%[^\n]s”,p->strada);
printf(“朱迪特:”);
scanf(“%[^\n]s”,p->judet);
printf(“ORAS:”);
scanf(“%[^\n]s”,p->oras);
printf(“VARSTA:”);
//%d用于int类型,此处需要(&d)
scanf(“%d”,&p->varsta);
}
void init(结构persoana*p)
{
//代码中最初的指针初始化是使用地址完成的
//字符串文本“ham”的一部分。使用它,之后将尝试写入
//取消引用的数据将导致未定义的行为或崩溃。
//现在,有了数组,就不再允许初始化了,
//所以strcpy用于复制数据
strcpy(p->numePrenume,“ham”);
strcpy(p->numePrenume,“ham”);
strcpy(p->strada,“ham”);
strcpy(p->judet,“ham”);
strcpy(p->oras,“ham”);
p->varsta=1;
}
空afis(结构人*p)
{
printf(“%s\n”,p->numePrenume);
printf(“%s\n”,p->strada);
printf(“%s\n”,p->judet);
printf(“%s\n”,p->oras);
printf(“%d\n”,p->varsta);
}
int main()
{
persoanap结构;
init&p;
afis&p;
花旗集团(citire&p),;
afis&p;
返回0;
}

你有很多没有先行词的代词。@DOUGLASO.MOEN:是的,它们是。不管这些人是什么样的人都是不正确的,为什么你说Prtff不是CPP?不起作用,结果更糟,:)我收到一些奇怪的信
#include <stdio.h>
#include <string.h>
struct persoana{
    // Declare members as char arrays (beware of the fixed size)
    // If declared as pointers, they have to be malloc'ated, maybe in the init function.
    char numePrenume[50];
    char strada[50], oras[50], judet[50];
    int varsta;
};
void citire(struct persoana *p)
{
    // For the variables of type array or pointer, they already represent
    // an address so "&" should not be used
    printf("NUME si PRENUME:");
    scanf(" %[^\n]s", p->numePrenume);
    printf("STRADA:");
    scanf(" %[^\n]s", p->strada);
    printf("JUDET:");
    scanf(" %[^\n]s", p->judet);
    printf("ORAS:");
    scanf(" %[^\n]s", p->oras);
    printf("VARSTA:");
    // %d used for int type, & needed here
    scanf(" %d", &p->varsta);

}
void init(struct persoana *p)
{
    // Original pointer initialization in your code was done using the addresses 
    // of string literals "ham". Using that, a write attempt afterwards to the 
    // dereferenced data would cause undefined behaviour or crash.
    // Now, having arrays, that initialization is no more permitted, 
    // so strcpy is used to copy data
    strcpy(p->numePrenume, "ham");
    strcpy(p->numePrenume, "ham");
    strcpy(p->strada, "ham");
    strcpy(p->judet, "ham");
    strcpy(p->oras, "ham");
    p->varsta = 1;
}
void afis(struct persoana *p)
{
    printf("%s\n", p->numePrenume);
    printf("%s\n", p->strada);
    printf("%s\n", p->judet);
    printf("%s\n", p->oras);
    printf("%d\n", p->varsta);
}

int main()
{
    struct persoana p;
    init(&p);
    afis(&p);
    citire(&p);
    afis(&p);
    return 0;
}