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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:09:54+00:00 2026-06-16T05:09:54+00:00

Say I have a simple function that returns a C string this way: const

  • 0

Say I have a simple function that returns a C string this way:

const char * getString()
{
  const char * ptr = "blah blah";
  return ptr; 
}

and I call getString() from main() this way:

  const char * s = getString();

1) According to gdb, the variable ptr is stored on the stack, but the string pointed by ptr is not:

(gdb) p &ptr
$1 = (const char **) 0x7fffffffe688

(gdb) p ptr
$2 = 0x4009fc "blah blah"

Does this mean that “blah blah” is not a local variable inside getString()?

I guess that if it were a local variable, I would not be able to pass it to my main() function…
But if it’s not, where is it stored? On the heap? Is that a “kind of” dynamically memory allocation implemented by the OS every time it hits on a string, or what?

2) If I use an array instead of a pointer, this way:

const char *getString2()
{
  const char a[] = "blah blah blah";
  return a;
}

the compiler warns me that:

warning: address of local variable ‘a’ returned

(and of course the program compiles, but it doesn’t work).

Actually, if I ask gdb, I get

(gdb) p &a
$2 = (const char (*)[15]) 0x7fffffffe690

But I thought that const char * ptr and const char a[] were basically the same thing. Looks like they’re not.

Am I wrong? What is exactely the difference between the two versions?

Thank you!

  • 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-16T05:09:55+00:00Added an answer on June 16, 2026 at 5:09 am

    When you write

    const char *ptr = "blah blah";
    

    then the following happens: the compiler generates a constant string (of type char []) with the contents "blah blah" and stores it somewhere in the data segment of the executable (it basically has a similar storage duration to that of variables declared using the static keyword).

    Then, the address of this string, which is valid throughout the lifetime of the program, is stored in the ptr pointer, which is then returned. All is fine.

    Does this mean that "blah blah" is not a local variable inside getString()?

    Let me respond with a broken English sentence: yes, it isn’t.

    However, when you declare an array, as in

    const char a[] = "blah blah";
    

    then the compiler doesn’t generate a static string. (Indeed, this is a somewhat special case when initializing strings.) It then generates code that will allocate a big enough piece of stack memory for the a array (it’s not a pointer!) and will fill it with the bytes of the string. Here a is actually a local variable and returning its address results in undefined behavior.

    So…

    But I thought that const char *ptr and const char a[] were basically the same thing.

    No, not at all, because arrays are not pointers.

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

Sidebar

Related Questions

Let's say I have a simple function like this: public static string randomthing(string var1)
Lets say i have a function that using regular expressions cleans a string: function
let's say I have a simple javascript like so: $(document).ready(function(){ if(login == 1) {
Let's say I have some simple Javascript like: <script> var hello = function(){ alert(Hello
Let's say I have the following, very simple block of code: <script> function hasVal(field)
Say I have simple program that emulates a board game with a number of
Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0;
Say I have a simple table that contains username, firstname, lastname. How do I
I have this simple function which I pass in an array of strings: function
So, let's say I have this code (VB.Net): Sub Main() dim xxx as string

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.