Thread: Difference between char *str1 & const char *str2

  1. #1
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49

    Difference between char *str1 & const char *str2

    Gudday all
    On a previous thread Extra characters in string I was given some help to extract and correctly terminate strings.
    My next step is to concatenate two strings, comprising a path and a file name, and then move the file to the new location using rename() and so move the file to its new location.
    I thought that I would make a new variable to hold the concatenated strings (path and file) and then put that new variable into rename().
    Code:
    #include <cstring.h>
    const size_t size = 64;
    ...
    char move_tiff_file[size+1] = {0};  //final destination of TIFF file  /* this var. added 05/08 */
    ...
    /* these next 2 lines added 05/08 */
    move_tiff_file = strncat(dest, tiff_file, size);
    rename(tiff_file, move_tiff_file);
    When I build the code I get the error message in the strncat line " error: incompatible types in assignment of `char*' to `char[65]' ".
    Looking at a reference site for C and looking up strncat I see the following
    #include <cstring>
    char *strncat( char *str1, const char *str2, size_t count );
    dest and tiff_file are both char*

    Q: What is the difference between char *str1 & const char *str2?
    Q: What is wrong with either my definition of move_tiff_file or my handling of strncat?

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Const has nothing to do with your issue. You are trying to assign a pointer to array address. You can't do this, but you can do vice versa:

    Code:
    char ff[10];
    char *pp
    
    pp = ff; //ff is similar to &ff[0]
    Next,
    strncat(dest, tiff_file, size);
    already means that concntenated string ends up in dest, and dest value is actually what gets returned. Either use dest, or strcpy(move_tiff_file, dest); to achieve your goal.
    Last edited by GL.Sam; 08-04-2009 at 08:03 PM.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An array is NOT a pointer. Remember those words.
    Furthermore, you CANNOT assign anything to an array. Remember that too.
    So the problem is that you're trying to assign a pointer to an array, which is a no-no.
    Another interesting thing is that if you put the array name on the right-hand-side of a assignment, you will get a pointer.

    And lastly, the const part: const simply means it cannot be changed.
    So char* is a pointer to char, which you can change.
    Const char* is a pointer to const char - in other words, one or more chars which you cannot change.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    The difference is that the array actually has no pointer pointing to it. The compiler saves the location of the beginning of the array and can convert it to a pointer, but it can't modify it.

    "arr" is exactly equivalent to "&arr[0]" and results in a rvalue pointer. I think it's like taking the address of any other local variable. You can't do "&i = malloc(sizeof(int))", can you?
    Last edited by Brafil; 08-06-2009 at 03:54 AM.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    &arr[0]=&(*arr)=arr
    &* cancel each other

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM