Assigning Const Char*s, Char*s, and Char[]s to wach other

This is a discussion on Assigning Const Char*s, Char*s, and Char[]s to wach other within the Linux Programming forums, part of the Platform Specific Boards category; There is one thing that i always have problems with. When i use char*s exclusively, i get segmentation faults. When ...

  1. #1
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281

    Assigning Const Char*s, Char*s, and Char[]s to wach other

    There is one thing that i always have problems with.

    When i use char*s exclusively, i get segmentation faults.
    When i use char[]s exclusively, i run into problems sending them to functions.

    When I use both, I run into both problems, along with how to convert bwtween the two.

    How do you assign a char* to the contents of a const char*?
    How do you assign a char* to the contents of a char[]?
    How do you assign a char[] to the contents of a const char*?
    How do you assign a char[] to the contents of a char*?

    Thank you very much. Any othe helpful hints on when to use which would be helpful.

    ~Inquirer
    Last edited by Inquirer; 04-29-2003 at 09:42 PM.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    http://pw1.netcom.com/~tjensen/ptr/pointers.htm

    A pointer by itself doesn't have any memory - so if you try and use these 'off the bat', then you're in for many seg faults.

    Eg
    fgets() has the following prototype
    char *fgets(char *s, int n, FILE *stream);

    So
    char *foo;
    fgets( foo, 100, stdin );
    looks good from the types, foo doesn't point anywhere useful.

    This improves the situation - foo now points to some legal memory
    char *foo = malloc( 100 );
    fgets( foo, 100, stdin );

    http://www.eskimo.com/~scs/C-faq/s6.html
    Now you can also use an array in this context
    char foo[100];
    fgets( foo, 100, stdin ); // foo == &foo[0] by value as well as by type (namely a char*)
    Arrays always have memory associated with them, so wherever you have a function expecting a char* parameter, it's always a safe bet to use an array.

    Assignments
    One of the differences between arrays and pointers is that you can assign to pointers, but you can't assign arrays
    char *foo;
    char bar[10];

    You can say foo = bar; but you can't say bar = foo;
    In the case of foo = bar; all you're doing is making foo[i] reference the same memory locations as bar[i]

    Here's where you have to be careful
    char *foo = malloc( 10 );
    char bar[10] = "hello";

    The WRONG thing to do is foo = bar;
    Initially, it seems to work, because if you try and print foo, it will print "hello", and all will be well. HOWEVER, if you try and free(foo), you're in for a shock, and if you change bar, foo will change with it (which can seem kinda odd if you don't expect it)
    The RIGHT thing to do is strcpy( foo, bar );

    > Any othe helpful hints on when to use which would be helpful.
    Write a few small programs, post them here for review and we'll tell you what mistakes you've made. It's the only real way for us to see which particular details you're having problems with.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21