Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
C char* printf issue

I am facing a strange behavior with a char* variable

user_message* parseMessage(char *incoming_msg, uint64_t size)
{
    user_message* msg = calloc(1, sizeof(user_message));
    printf("value: %s\n", incoming_msg);
    return msg;
}
void start_server()
{
    char* msg = "1|david|pwd|";
    printf("msg: %s\n", msg);
    parseMessage(&msg, 12);
}

The output :

msg: 1|david|pwd|
value: �[

I struggle to figure out what is wrong in my code.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

parseMessage(&msg, 12); should be parseMessage(msg, 12);

The &msg means you're passing a pointer to msg, which will be a char ** when the function takes only a char *

over 4 years ago · Santiago Trujillo Report

0

The first parameter of the function parseMessage and the expression used as an argument for this parameter have different types.

The parameter is declared as having the type char *

user_message* parseMessage(char *incoming_msg, uint64_t size)

while the expression used as an argument has the type char **.

char* msg = "1|david|pwd|";
//...
parseMessage(&msg, 12);

So in fact instead of outputting the string pointed to by the pointer msg you are trying to output as a string what is stored in memory occupied by the variable msg and what is followed it

You need to call the function at least like

parseMessage( msg, 12 );

Also it would be better at least to declare the first function parameter with the qualifier const if the passed string is not changed within the function

user_message* parseMessage( const char *incoming_msg, uint64_t size);
over 4 years ago · Santiago Trujillo Report

0

&msg yields a char **, or a pointer-to-pointer-to-char.

Both your parseMessage function, and the printf specifier %s expect a char *.

You don't need the address-of operator here

parseMessage(msg, 12);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!