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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:29:15+00:00 2026-06-17T04:29:15+00:00

Possible Duplicate: Difference between – returning a ‘local’ char* from a function vs returning

  • 0

Possible Duplicate:
Difference between – returning a ‘local’ char* from a function vs returning a ‘local’ int* from a function

Here is a simple code, where in 3 different functions [ localStrPtr, localIntPtr, localCharPtr] return a pointer to their local variables [string, integer, char] in their respective functions.

CODE:

#include <stdio.h>

char*  localStrPtr (char*);
int*   localIntPtr (int, int);
char*  localCharPtr (char);

main()
{
    int *pInt;
    char *pChar;

    printf( "localStrPtr = %s\n", localStrPtr("abcd") );

    pInt = (int*) localIntPtr(3, 5);
    printf( "localIntPtr = %d\n", *pInt );

    pChar = (char*) localCharPtr('y');
    printf( "localCharPtr = %c\n", *pChar );
}

char* localStrPtr(char* argu)
{
    char str[20];
    // char* str = (char*) malloc (20);

    strcpy (str, argu);
    return str;
}

int* localIntPtr (int argu1, int argu2)
{
    int local;
    local = argu1 + argu2;
    return (&local);
}

char* localCharPtr (char argu)
{
    char local;
    local = argu;
    return (&local);
}

COMPILE LOG:

stringManip.c: In function `localStrPtr':
stringManip.c:27: warning: function returns address of local variable
stringManip.c: In function `localIntPtr':
stringManip.c:34: warning: function returns address of local variable
stringManip.c: In function `localCharPtr':
stringManip.c:41: warning: function returns address of local variable

RUN LOG:

localStrPtr =
localIntPtr = 8
localCharPtr = y

As you can see in the log file, localStrPtr returns “some garbage”, whereas localIntPtr and localCharPtr return “expected” values.

But, in the function localStrPtr, if I change char str[20] -to-> char* str = (char*) malloc (20), localStrPtr returns the string “abcd” correctly. Here is the RUN LOG, once the above change is made.

NEW RUN LOG:

localStrPtr = abcd
localIntPtr = 8
localCharPtr = y

QUESTIONS:

[1] In functions localIntPtr and localCharPtr, contents of the returned local variable addresses WORKED, but for the function localStrPtr, correct value is returned “only” with malloc, but will not with local char str[20]. Why doesn’t it work with str[20] while works with local variables char and int ?

[2] Why do we see in the COMPILE LOG, the lines below for all the 3 functions ?

  • stringManip.c:27: warning: function returns address of local variable
  • stringManip.c:34: warning: function returns address of local variable
  • stringManip.c:41: warning: function returns address of local variable
  • 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-17T04:29:17+00:00Added an answer on June 17, 2026 at 4:29 am

    As the compiler mentions you are returning an address on the stack. After you do the return the value in the stack may or may not be overwritten by some other function call coming in later. The fact that you get an int or a char is just chance. if you issue another printf( "localIntPtr = %d\n", *pInt ); just before your return again you are likely to see that the pInt has been overwritten already.

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

Sidebar

Related Questions

Possible Duplicate: Difference between (function(){})(); and function(){}(); I am trying to understand a few
Possible Duplicate: Difference between void main and int main? Why is void main() {
Possible Duplicate: Difference between void main and int main? Alright, so I'm using bloodshed
Possible Duplicate: Difference between Property and Field in C# .NET 3.5+ Sample code: public
Possible Duplicate: difference between Convert.ToInt32 and (int) What it difference between (int) and convert.toint()
Possible Duplicate: Difference between try-catch syntax for function Few days ago I was reading
Possible Duplicate: Difference between (function(){})(); and function(){}(); Are (function ( ) { } )
Possible Duplicate: Difference Between $.getJSON() and $.ajax() in jQuery super simple question.... between .getjson
Possible Duplicate: Difference between these two conditions? I am doing some code cleanup and
Possible Duplicate: Difference between if () { } and if () : endif; Simple

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.