Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. basic c/c++ question (const related)

basic c/c++ question (const related)

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Griffing
    wrote on last edited by
    #1

    I'd just like to make sure of something that I'm kind of worried about. This is not ok: char *Foo() { char ret[100] = "hello"; return ret; } This is not ok: char *Foo() { char ret[100] = "hello"; return ret; } This is ok: const char *Foo() { return "hello world"; } main() { printf(Foo()); } Is this right? Thanks!

    L M 2 Replies Last reply
    0
    • A Alex Griffing

      I'd just like to make sure of something that I'm kind of worried about. This is not ok: char *Foo() { char ret[100] = "hello"; return ret; } This is not ok: char *Foo() { char ret[100] = "hello"; return ret; } This is ok: const char *Foo() { return "hello world"; } main() { printf(Foo()); } Is this right? Thanks!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      return "hello world";

      is fine because the string literal "hello world" is stored in the executable image, and stays allocated for the life of your program. --Mike-- http://home.inreach.com/mdunn/ "Holding the away team at bay with a non-functioning phaser was an act of unmitigated gall. I admire gall."   -- Lt. Cmdr. Worf

      1 Reply Last reply
      0
      • A Alex Griffing

        I'd just like to make sure of something that I'm kind of worried about. This is not ok: char *Foo() { char ret[100] = "hello"; return ret; } This is not ok: char *Foo() { char ret[100] = "hello"; return ret; } This is ok: const char *Foo() { return "hello world"; } main() { printf(Foo()); } Is this right? Thanks!

        M Offline
        M Offline
        Malcolm McMahon
        wrote on last edited by
        #3

        When you say char anArray[200] you are saying "Grab me 200 characters worth of local space and call it "anArray". You could them go on to put "Hello World" into it if you wanted, but only by copying it in character by character, for example with: strcpy(anArray, "Hello World!"); However because the space for anArray is local it's discarded as soon as your function exits. A pointer returned to it will point to garbage, because that pointer looses it's validity when the routine exits. The string constant "Hello World" is a different kind of beasty. The string is stored by the compiler in static storage. It's address remains valid for the life of the program. When you say, for example, const char *p = "Hello World"; You are allocating a pointer. A memory location containing the address of that static string. Not a new block of memory containing the string itself (which is why it's ok to do a simple assignment in such a case rather than a more complex copy). Does that make things clearer?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups