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 8462347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:08:30+00:00 2026-06-10T14:08:30+00:00

I have program use c and c++ language to access data from MySQL database.

  • 0

I have program use c and c++ language to access data from MySQL database.
The codes :

const char *query="SELECT * FROM myTable;";
printf("%s\n",query);
state=mysql_query(sock,query);
printf("%s\n",query);

from that code, query value from before and after ‘state’ statement is same (SELECT * FROM myTable;). but for this code:

const char *query=getQuery();
printf("%s\n",query);
state=mysql_query(sock,query);
printf("%s\n",query);

the function:

const char * getQuery(){
 const char *returnValue;
 char q[BUFSIZ];
 sprintf_s(q,"%s","SELECT * FROM myTable;");
 returnValue=q;
 return returnValue;
}

from this code, query value from before and after “state” statement is not same,before (SELECT * FROM myTable;) and after (1/4>-uC^M).

Anybody know that?

thanks in advance.

  • 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-06-10T14:08:32+00:00Added an answer on June 10, 2026 at 2:08 pm

    Problem is you are returning a local variable.
    After the function getQuery() has been executed, q will be gone and the pointer u return points to invalid memory and reads gibberish.
    The reason it works before and not after the query is because the data hasn’t been overwritten yet.

    what you want is somthing like this

    char *returnvalue = (char *) malloc(sizeof(char)*BUFSIZ);
    char q[BUFSIZ];
    sprintf_s(q,"%s","SELECT * FROM myTable;");
    memcpy(returnvalue, q, BUFSIZ);
    return returnvalue;
    

    or this

    char *returnvalue = (char *) malloc(sizeof(char)*BUFSIZ);
    sprintf_s(returnvalue ,"%s","SELECT * FROM myTable;");
    return returnvalue;
    

    After the query you have to remember to free the allocated memory somewere.

    free(query);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To access a remote database, I first have to use a program called Shrew
I have made a registration program. Making use of mysql database. Can I still
I have a program that makes use of the following method to get a
I have a flight reservation program use mssql ,For reserving flights i want to
Suppose I have a program that initializes a global variable for use by threads,
I have a program that creates semaphore. But when i try to use SETALL,
I have my own installer program which I use to install several applications I
so in my program I have parts where I use try catch blocks like
I have a unstable program that kept crashing randomly, I could use a bash
I have a VS2008 C++ program where I'm wrapping a C API for use

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.