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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:32:44+00:00 2026-06-02T11:32:44+00:00

I have came across this problem when using pointer to pointer to a char:

  • 0

I have came across this problem when using pointer to pointer to a char:

void setmemory(char** p, int num)
{
    *p=(char*)malloc(num);
}

    void test(void)
    {
        char* str=NULL;
        setmemory(&str,100);
        strcpy(str,"hello");
        printf(str);
    }

int main()
{
    test();
    return 0;
}

The code above is correct,but I can’t figure it out why using a pointer to a pointer char** p here? Why just using a pointer to a char instead? so I change this snippet into below and found it not working ,can anyone tell me why? thanks!

void setmemory(char* p, int num)
{
    p=(char*)malloc(num);
}

void test(void)
{
    char* str=NULL;
    setmemory(str,100);
    strcpy(str,"hello");
    printf(str);
}

int main()
{
    test();
    return 0;
}
  • 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-02T11:32:45+00:00Added an answer on June 2, 2026 at 11:32 am

    One thing to note here is – When we say pointers, we generally tend to think in terms of pass by reference but not necessarily. Even pointers can be passed by value

    char* str is local to test and char* p is local to setmemory . So the changes you do in setmemory will not be visible in test if you dont send a pointer to a pointer.

    You can make it work with a single pointer like this

     char * setmemory(char* p, int num) // p is a new pointer but points at the same
                                        // location as str
    {
        p=(char*)malloc(num); // Now, 'p' starts pointing at a different location than 'str'
        strcpy(p ,"hello");  // Copy some data to the locn 'p' is pointing to
        return p; // Oops. The poor `str` is still pointing at NULL :( 
                  // Send him the address of the newly allocated area
    }
    
    void test(void)
    {
        char* str=NULL;
        str=setmemory(str,100); // We are passing a pointer which is pointing to NULL
    
        printf(str); //Now str points to the alloced memory and is happy :)
    }
    
    int main()
    {
        test();
        return 0;
    }
    

    Note that in setmemory we are returning a local pointer, but it is not a problem ( no dangling pointer problems ) as this pointer points to a location on heap and not on stack

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

Sidebar

Related Questions

Today I came across this question: you have a code static int counter =
I have been using Perl for some time, but today I came across this
I came a across a new problem when using templates. This is me being
I have came across this problem a few times and can't seem to figure
I came across this code on reddit . I would have thought that type
I was doing a bit research and I came across this comment. I have
I was practicing SRM Problems in Topcoder.I came across this problem Problem Statement: Today
I just came across this weird problem that is happening in IE. In the
I have came across very interesting problem which I think it's on Salesforce's end
I have been experimenting with GNU Radio and came across the tunnel.py program. This

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.