简体   繁体   English

将字符分配给 c 中的箭头指针

[英]Assign characters to arrow pointer in c

struct room
{
    
    char venue[15];  //variable
    
    
    
}
 *stream;
.
.
.
void idealounge(){
    int i,idealounge[10];
    
    char room1[10]="MMLC";

    ***  **  stream->venue = room1;*****
    
      
    }

This is a room booking system.这是一个房间预订系统。

The name of the room called MMLC.房间的名称称为 MMLC。

I expect it can store "MMLC" to stream->venue but it shows errors like "incompatible values" etc.我希望它可以将“MMLC”存储到 stream->venue 但它会显示“不兼容值”等错误。

The statement:该声明:

char venue[15];

declares venue to be an array of size 15 of char .venue声明为大小为 15 的char数组。 Then,然后,

char room1[10]="MMLC";

initialises the first 4 bytes of room1 with the string literal "MMLC" , setting the rest to \0 .使用字符串文字"MMLC"初始化room1的前 4 个字节,将 rest 设置为\0

Then,然后,

stream->venue = room1;

is invalid, because venue is an array, not a pointer.无效,因为venue是一个数组,而不是指针。 Arrays are not modifiable l-values. Arrays 是不可修改的左值。 You can't assign arrays like this.您不能像这样分配 arrays。

If you want to copy the contents of room1 to the contents of venue , use standard strcpy() .如果要将room1的内容复制到venue的内容,请使用标准strcpy()


That being said,话虽如此,

struct room
{
    char venue[15];  //variable      
}
 *stream;

only allocates space for the pointer, which is uninitialized and isn't pointing to anything meaningful.只为未初始化且未指向任何有意义的指针分配空间。 So first allocate memory and initialise the pointer:¹所以首先分配 memory 并初始化指针:¹

stream = malloc (sizeof (struct room));

Then check if it succeeded:²然后检查是否成功:²

if (!stream) {
    perror ("malloc()"); 
    /* handle error here...*/
}

Now perform the string copy.现在执行字符串复制。


Alternatively, you can allocate the struct with automatic storage duration, which is simpler and less error-prone:或者,您可以分配具有自动存储持续时间的struct ,这更简单且不易出错:

struct room bedroom;

and then assign it's address to the pointer:然后将它的地址分配给指针:

stream = &bedroom;

This avoids the need for dynamic memory allocation, which you might fail to free() later on.这避免了动态 memory 分配的需要,您稍后可能无法free()


[1]NB that I do not cast the result of malloc() . [1]注意我没有malloc()的结果。 malloc() returns a generic void * , or void pointer, which is automatically promoted to the correct type. malloc()返回一个通用的void *void指针,它会自动提升为正确的类型。 So there's no need to cast, and doing so might hide a potential bug.所以没有必要投射,这样做可能会隐藏一个潜在的错误。

See also: Do I cast the result of malloc?另请参阅:我是否投射 malloc 的结果?

[2]POSIX-compliant systems set errno on malloc() failure, the above code snippet assumes it does. [2]POSIX 兼容系统在malloc()失败时设置errno ,上面的代码片段假定它确实如此。 But you may not wish to take it for granted, and handle the error in another way.但您可能不希望认为这是理所当然的,并以另一种方式处理错误。

struct room
{
    char venue[15];  //variable
}
 *stream = NULL;
 
void idealounge() {
    int i;
    int idealounge[10];
    
    char room1[10]="MMLC";
    stream = (struct room *)malloc(sizeof(struct room));
    strcpy(stream->venue, room1);
}

You should write this way.你应该这样写。

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

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