In this examle in writer.c I am trying to generate symbols from a to z and then print them in uppercase in reader.c.
writer.c:

// C Program for Message Queue (Writer Process) 
#include <stdio.h> 
#include <sys/ipc.h> 
#include <sys/msg.h> 
#include <string.h>

// structure for message queue 
struct mesg_buffer { 
    long mesg_type; 
    char mesg_text[100]; 
} message; 

int main() 
{ 
    key_t key; 
    int msgid; 

    // ftok to generate unique key 
    key = ftok("progfile", 65); 

    // msgget creates a message queue 
    // and returns identifier 
    msgid = msgget(key, 0666 | IPC_CREAT); 
    message.mesg_type = 1; 

char c;

    for(c = 'a'; c <= 'z'; ++c)

strcpy(message.mesg_text,c);

    // msgsnd to send message 
    msgsnd(msgid, &message, sizeof(message), 0); 

    // display the message 
    printf("Data send is : %s \n", message.mesg_text); 

    return 0; 

} 

reader.c

    // C Program for Message Queue (Reader Process) 
#include <stdio.h> 
#include <sys/ipc.h> 
#include <sys/msg.h> 

// structure for message queue 
struct mesg_buffer { 
    long mesg_type; 
    char mesg_text[100]; 
} message; 

int main() 
{ 
    key_t key; 
    int msgid; 

    // ftok to generate unique key 
    key = ftok("progfile", 65); 

    // msgget creates a message queue 
    // and returns identifier 
    msgid = msgget(key, 0666 | IPC_CREAT); 

    // msgrcv to receive message 
    msgrcv(msgid, &message, sizeof(message), 1, 0); 

       for (int i=0; i<100; ++i)
            message.mesg_text[i] =  toupper(message.mesg_text[i]); 

    // display the message 
    printf("Data Received is : %s \n",  
                    message.mesg_text); 

    // to destroy the message queue 
    msgctl(msgid, IPC_RMID, NULL); 

    return 0; 
} 

When I run the writer.c I get this message:

222.png

Any ideas how to fix it?

In the first code passage, you declare a char called c, then on line 30 treat this char as if it was an array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.