Thread: Initialization of Char Pointers?

  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    Initialization of Char Pointers?

    I don't understand how this works:
    Code:
    char *set = "abcdefghijklmnopqrstuvwxyz";
    Does the c compiler magically convert that into:
    Code:
    char *set = malloc(28 * sizeof(*set));
    strcpy(set, "abcdefghijklmnopqrstuvwxyz"
    ??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No.

    It's more like
    Code:
    const char anonymous_name_generated_by_compiler[] = "abcdefghijklmnopqrstuvwxyz";
    char *set = anonymous_name_generated_by_compiler;
    The "characters" are used to fill in an array of chars, and then the assignment makes a pointer to the first element of that array.

    Code:
    const char temp[] = "abcdefghijklmnopqrstuvwxyz";
    char *set = temp;
    Is almost the same thing (except you don't have to create a named location for the string)
    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
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The main difference is that with malloc()'d memory, you can modify the string later on at some point. But the memory that is pointed to using char *set = "some string" can't be modified later on (i.e. you couldn't do set[0] = 'r').
    If you understand what you're doing, you're not learning anything.

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    itsme86 : you should take a looong vacation and give us a chance to reply to some threads
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    posted by kleid-0
    Code:
     Does the c compiler magically convert that into:
    
    Code:
    char *set = malloc(28 * sizeof(*set));
    strcpy(set, "abcdefghijklmnopqrstuvwxyz");

    Code:
                          yes !!!                   

  6. #6
    Um, compilers don't use magic. His first code is a string literal. His second is using a block of memory allocated to it.

    The first code does not magically act like the second. The behavior's are different, and respond differently.

    Want to make sure? Use the function free() on your second code after you malloc() it. Did it crash? Probably not. Try it on your first code. Did it crash? Yes. Because a string literal isn't your memory, so you can't modify it nor free it.

    Big difference.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Stack Overflow
    Um, compilers don't use magic.
    I beg to differ: http://jargon.watson-net.com/jargon.asp?w=automagically

    If you understand what you're doing, you're not learning anything.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Once again, enjoy shows how little he knows...
    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.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    But at least they used code tags! Oh, wait...

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    /b] sorry [/b]
    Code:
    but i'm a student not a professonal programmer ... !

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by enjoy
    /b] sorry [/b]
    Code:
    but i'm a student not a professonal programmer ... !
    I know Quzah's humor is very subtle, but YOU REALLY DON"T NEED TO PUT CODE TAGS AROUND YOUR REPLIES.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Use code tags plz, when using code.
    -------
    Quote Originally Posted by Salem
    Code:
    const char temp[] = "abcdefghijklmnopqrstuvwxyz";
    char *set = temp;
    I see now, thank you very much Salem :)
    Last edited by Kleid-0; 01-07-2005 at 06:55 PM.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Kleid-0
    Who cares if they're freakin' code tags? It's not that big of a deal.
    We care. The creator of the forum cares. Everyone who has to read unindented piles of steaming (I am sillyI am sillyI am sillyI am silly) care. Use them or ... well find out fast that no one will help you ever again.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Ok I understand, I edited my last post. I need to start being more positive here.

  15. #15
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by itsme86
    The main difference is that with malloc()'d memory, you can modify the string later on at some point. But the memory that is pointed to using char *set = "some string" can't be modified later on (i.e. you couldn't do set[0] = 'r').
    You should point out (no pun intended) that char *set is immutable but it can still point to another area in memory. Not a big thing but helpful
    Code:
    char *set = "some string"
    set = "now i'm changed"
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM