Thread: scope

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    scope

    Hello,

    Quick question about scope in C: if I have the following:

    Code:
    int* p;
    
    {
      int x = 1;
      p = &x;
    }
    
    //do something with p
    then there is no guarantee that *p == 1, since x is destroyed after the end of the code block.

    However if I had:

    Code:
    char* p;
    
    {
      char* x = "hello world";
      p = x;
    }
    
    //do something with p
    would that have the same instability? If I had used malloc() instead, would that keep the value 'alive' in memory?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > then there is no guarantee that *p == 1, since x is destroyed after the end of the code block.
    Correct.

    > would that have the same instability?
    No, the string constant is in persistent memory. x and p are just copies of pointers to that memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Also note that you should always use const char* when you want a pointer to a string literal. (It is unmodifiable, but the type system for some legacy reasons lets you get away with removing the const.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Quote Originally Posted by anon View Post
    Also note that you should always use const char* when you want a pointer to a string literal. (It is unmodifiable, but the type system for some legacy reasons lets you get away with removing the const.)

    But then why is it that:

    Code:
    char* x = "hello";
    x = "world";
    printf(x);
    prints "world" without issue?

    Would arrays of say, integers, face the same constraints as strings?

    Thanks


    edit. also,

    char x[] = "hello";
    x[2] = 'k';
    printf(x);

    produces 'heklo' as its output? how does this work?
    Last edited by beereo; 03-17-2010 at 06:33 AM. Reason: Further question

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    In the first case you modify the POINTER, not the actual string.

    In the second case you do modify the string, and the behaviour is undefined. For you it works. For me, it'd probably make the application crash.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by EVOEx View Post
    In the second case you do modify the string, and the behaviour is undefined. For you it works. For me, it'd probably make the application crash.
    The second case the op uses is an array not a pointer. The behavior is not undefined. If it had been a pointer then yes.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by hk_mp5kpdw View Post
    The second case the op uses is an array not a pointer. The behavior is not undefined. If it had been a pointer then yes.
    Ahh sorry, I misread it (I'll blame it on the lack of code tags :P). I stand corrected .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C name space and lexical scope
    By password636 in forum C Programming
    Replies: 1
    Last Post: 09-22-2009, 09:50 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM