Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6223665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:31:46+00:00 2026-05-24T08:31:46+00:00

I am having some problems when returning with printf of a char array from

  • 0

I am having some problems when returning with printf of a char array from a struct in C.

struct q_entry
{
    long mtype;
    char mtext[MAXLENGTH + 1];
};

The long mtype from the struct is returning fine, but the string is just returning some weird characters.

int proc_obj(struct q_entry *msg)
{
    printf("\npriority: %ld name: %s\n", msg->mtype, msg->mtext);
}

It just returns some strange characters like “priority: 1 name: ▒▒(” and not “priority: 1 name: hello”

I am populating the struct using the following code

int enter(char *objname, int priority)
{
    ...

    strncpy(s_entry.mtext, objname, sizeof(s_entry.mtext) - 1);
    s_entry.mtype = priority;

    // Send the message
    if (msgsnd(s_qid, &s_entry, len, 0) == -1)
    {
        printf("error: msgsnd failed\n");
        return(-1);
    }
    else
    {
        return(0);
    }
}

I don’t have much experience with C, so I don’t know too much about using structs. Please let me know if more context or parts of the code is needed. Any kind of help would be very helpful.

I have added a little more code in enter above, and here is more code of the when enter and proc_obj are called

main(int argc, char **argv)
{
    int priority;

    if (argc != 3)
    {
        printf("error: incorrect number of arguments\n");
        exit(1);
    }
    else
    {
        priority = atoi(argv[2]);
    }

    if (enter(argv[1], priority) < 0)
    {
        printf("error: message entering failed\n");
        exit(1);
    }

    exit(0);
}

This is in a different file from enter and above code

int server(void)
{
    int mlen, r_qid;
    struct q_entry r_entry;

    // Initialize queue
    if ((r_qid = init_queue()) == -1)
        return(-1);

    for (;;)
    {
        if ((mlen = msgrcv(r_qid, &r_entry, MAXLENGTH, (-1 * MAXPRIOR), MSG_NOERROR)) == -1)
        {
            printf("error: msgrcv failed\n");
            exit(1);
        }
        else
        {
            proc_obj(&r_entry);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:31:47+00:00Added an answer on May 24, 2026 at 8:31 am

    Answer before adding more code

    It looks like the s_entry structure object is local, and enter works on the local variable. How are you calling enter and returning the structure after you have done initializing it ? note that you have int as the return type of the enter function. If you are doing return s_entry; then the output you are getting is possible, as only the first word of the structure, ie the lower sizeof (int) part of mtype is considered.

    If you are using enter function like this as i described above then make the return type of enter to struct s_entry


    You should check the size of len when sending the message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problems on returning the parameter of a method as a template,
I'm having some problems calling a C function from a DLL, and was hoping
I'm having problems returning a Session value set from mocking using Moq. Using the
Currently having some problems- now = datetime.datetime.now() month = now.strftime(%B) site = wikipedia.getSite('en', 'wikiquote')
Im having some problems getting the Sticky Footer to work on my site. If
I'm having some problems integrating MS MapPoint 2009 into my WinForms .Net 2.0 application
I have been having some problems trying to get my PHP running. When I
I'm having some problems with the ranking used by fulltext search in SQL Server.
I'm having some problems with a datagridview element I'm using in VS2008. This DataGridView
I'm having some problems getting my object to gracefully fail out if an invalid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.