0
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.

I expect it can store "MMLC" to stream->venue but it shows errors like "incompatible values" etc.

G Dydy
  • 13
  • 2
  • `stream` is an uninitialized pointer to type `struct room`; As with any pointer, if you can't answer the question "*What valid memory address does my pointer hold as its value?*", you are going to be troubled by pointers for a long time. A couple of links that provide basic discussions of pointers that may help. [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102) (ignore the titles, the answers discuss pointer basics) – David C. Rankin Jan 28 '23 at 08:21

2 Answers2

2

The statement:

char venue[15];

declares venue to be an array of size 15 of char. Then,

char room1[10]="MMLC";

initialises the first 4 bytes of room1 with the string literal "MMLC", setting the rest to \0.

Then,

stream->venue = room1;

is invalid, because venue is an array, not a pointer. Arrays are not modifiable l-values. You can't assign arrays like this.

If you want to copy the contents of room1 to the contents of venue, use standard 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:¹

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 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.


[1]NB that I do not cast the result of malloc(). malloc() returns a generic void *, or void pointer, which is automatically promoted to the correct type. So there's no need to cast, and doing so might hide a potential bug.

See also: Do I cast the result of malloc?

[2]POSIX-compliant systems set errno on malloc() failure, the above code snippet assumes it does. But you may not wish to take it for granted, and handle the error in another way.

Harith
  • 4,663
  • 1
  • 5
  • 20
0
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.

alamin39
  • 151
  • 1
  • 8