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

  • SEARCH
  • Home
  • 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 8873681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:31:35+00:00 2026-06-14T18:31:35+00:00

Can somebody help me explaining the following code: Why the char *s doesn’t receive

  • 0

Can somebody help me explaining the following code:

Why the char *s doesn’t receive the point location of memory allocated at foo()?

#include <stdio.h>
#include <stdlib.h>

char *foo()
{
    char *s = (char *)malloc(20);
    s = "Hello Heap.";
    return s;
}

void bar(char *s)
{
    s = foo();
    printf("bar: %s\n", s); // Works fine just as expected.
}

int main()
{
    char *s;
    bar(s);
    printf("%s\n", s); // Output some undefined content like `H?}?H??`, other than `Hello Heap.`
}
  • 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-14T18:31:36+00:00Added an answer on June 14, 2026 at 6:31 pm

    The code fixed

    #include <stdio.h>
    #include <stdlib.h>
    
    char *foo()
    {
        char *s = (char *)malloc(20);
        strcpy(s,"Hello Heap.");
        return s;
    }
    
    void bar(char **s)
    {
        *s = foo();
        printf("bar: %s\n", *s); // Works fine just as expected.
    }
    
    int main()
    {
        char *s;
        bar(&s);
        printf("%s\n", s); // Output some undefined content like `H?}?H??`, other than `Hello Heap.`
    }
    

    Explanation:

    1) Your code contains:

    char *s = (char *)malloc(20);
     s = "Hello Heap.";
    

    This not good. In this way you are not copying the "Hello Heap." message into the allocated memory. In fact you have pointed the s pointer to allocated memory and then you have pointed your pointer to a constant string address

    2) Your code contains

    void bar(char *s)
    {
        s = foo();
        printf("bar: %s\n", s); // Works fine just as expected.
    }
    

    in this function s is getting the pointer (pointer to allocated memory) from the foo() function. but you are not communicating the s pointer address to the function higher leve (main). to do you have to return the address of s at the end of the function or you can pass via address of address of pointer by using input parameter cha ** s

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

Sidebar

Related Questions

Can somebody help me in converting the following code written using if-else to try/catch.
Can somebody help me? The following code works, but unfortunately a function call to
Can somebody help me in converting the following java code to C#. Robot robot
can somebody help me with the following piece of the code: try: #do some
Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
Can somebody help me to convert the following array to a hash with the
Can somebody help me find the mistake I am doing in evaluating following XPath
Can somebody help me with this query.The page doesn't load up whenever i run
Can somebody help me or share some code about how to achieve GeoFencing on
Can somebody help me convert the following html <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul>

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.