简体   繁体   English

无法通过指针 [C] 为结构的变量赋值

[英]Can't assign value to a structure's variable via pointer [C]

I'm pretty new to the C.我对 C 很陌生。 I'm trying to create a simple program to represent a point using a structure.我正在尝试创建一个简单的程序来使用结构来表示一个点。 It looks like this:它看起来像这样:

// including standard libraries
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>

// including user defined libraries
;


typedef struct point {
    char p_name;
    double *p_coords;
} point_t;


int main() {

    point_t *pt;
    pt->p_name = "A";
    printf("%c", pt->p_name);



    // returning 0 if there are no errors
    return 0;

}

The problem is that, when I try to print the name of the point after I assigned the name "A" to it, the program does output nothing except for the exit code, which is (probably) a random number:问题是,当我在为它分配名称“A”后尝试打印点的名称时,程序除了退出代码之外什么都不做 output ,这(可能)是一个随机数:

Process finished with exit code -1073741819 (0xC0000005)

The fact is that pointers is a concept that is very hard for me to understand (I used to program in python before) and therefore I'm probably missing something.事实上,指针对我来说是一个很难理解的概念(我以前曾在 python 中编程),因此我可能遗漏了一些东西。 I've also tried out with other variable types such as int, but the result is the same (even the exit status number is the same).我也尝试过使用其他变量类型,例如 int,但结果是相同的(即使退出状态号相同)。 Is there a way to fix this behaviour?有没有办法解决这种行为?

PS: Excuse my rudimental English, I'm still practising it, and thanks a lot for your time! PS:对不起,我的初级英语,我还在练习,非常感谢你的时间!

you have to use malloc to allocate the memory of the poin_t structure.您必须使用 malloc 来分配 poin_t 结构的 memory。 Something like就像是

    point_t *pt = malloc(sizeof(point_t));
    pt->p_name = 'A';
    printf("%c", pt->p_name);

And very importantly as others mentioned is that pt->p_name = "a" is also wrong you are allocating int a char a const char* I fixed in my example正如其他人提到的那样,非常重要的是pt->p_name = "a"也是错误的,你正在分配 int a char a const char*我在我的示例中修复

In your code在您的代码中

  pt->p_name = "A";

is wrong for two primary reasons:错误有两个主要原因:

  1. You never made pt point to any valid memory location.您从未将pt指向任何有效的 memory 位置。 Attempt to dereference an invalid memory invokes undefined behavior.尝试取消引用无效的 memory 会调用未定义的行为。
  2. p_name is of type char . p_namechar类型。 "A" is a string literal, of type char [x] , which boils down to char * for assignment, and they are not compatible types. "A"是一个字符串文字,类型为char [x] ,归结为char *用于赋值,它们不是兼容的类型。

You need to你需要

  1. Make sure pt points to valid memory location.确保pt指向有效的 memory 位置。 Actually, you don;t need a pointer here, at all.实际上,这里根本不需要指针。 Define pt as a variable (not a pointer variable) of the structure type, and access the members via the .pt定义为结构体类型的变量(不是指针变量),并通过. operator.操作员。
  2. Use 'A' for assignment, as in character constant, not a string.使用'A'进行赋值,如字符常量,而不是字符串。

Pointers must be made to point somewhere.指针必须指向某处。 You never assign a value to the pointer pt , and attempting to dereference an uninitialized pointer value invokes undefined behavior .您永远不会为指针pt分配值,并且尝试取消引用未初始化的指针值会调用未定义的行为

You're also assigning a string to a character value.您还将字符串分配给字符值。 String use double quotes while single characters use single quotes字符串使用双引号,而单个字符使用单引号

Use single quotes for a character, and a pointer must first be made to point somewhere:对字符使用单引号,并且必须首先使指针指向某处:

point_t p;
point_t *pt = &p;
pt->p_name = 'A';
printf("%c", pt->p_name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM