CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2004
    Posts
    35

    what is the diff between char str[] and char * str ?

    Hi,
    Can any body tell me the exact diff between char * str and char str[] in C ?
    What exactly happens in memory when we declare these ?

    Regards,
    Naik

  2. #2
    Join Date
    Jan 2004
    Posts
    206

    Re: what is the diff between char str[] and char * str ?

    It's the same. Both create a variable "str" which is a pointer to char.

  3. #3
    Join Date
    Jul 2005
    Posts
    103

    Re: what is the diff between char str[] and char * str ?

    There is a topic on this already: Variable Pointer VS Array (Wai Wai) which answers your question.

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  5. #5
    Join Date
    Jan 2004
    Posts
    206

    Re: what is the diff between char str[] and char * str ?

    Quote Originally Posted by exterminator
    There's a difference. Have a look at this thread.
    Thanks for highlighting. SuperKoko 's answer is correct.

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: what is the diff between char str[] and char * str ?

    "Variable Pointer VS Array" show the difference between char[] and char* for local variable declarations, but you must know that it is very different for argument declarations.
    That is, for arguments there is no difference between char[] and char*:
    Code:
    int f(char str[])
    {
    std::cout<<str;
    str[0]='\0'; // modify the original string
    }
    Is identical to:
    Code:
    int f(char *str)
    {
    std::cout<<str;
    str[0]='\0'; // modify the original string
    }
    These two functions are identical, so you cannot use overloading:
    Code:
    int f(char []);
    int f(char *); // compiler error : "int f(char *)" already declared.
    Moreover if you try to have a linker error "undefined function", like that:
    Code:
    // test.cpp
    int f(char []);
    int main()
    {
    char str[8];
    f(str);
    return 0;
    }
    The linker (ilink32.exe, but i suppose that it may depend on the linker - i think that ilink32.exe is revelant on this point) outputs:
    Code:
    Error: Unresolved external 'f(char *)'
    It does not outputs 'f(char [])', but 'f(char *)'

    You can also put any positive integer between the '[' and the ']' characters, and there is always no difference.
    What you must absolutely know, is that you cannot pass an array by value.
    If you want to do so, you should define a struct containing the fixed size array:
    Code:
    struct CharArray8 // Obsolete - nowadays you should use std::vector - it is much better!
    {
    char Array[8];
    };
    On this point, i think that C++ is inconsistent (probably for compatibility with C which was also inconsistant on this point), so i never use this syntax, but only use the "char *" syntax.

    If you want to do some different processing on arrays and pointers, you must use a reference to an array such as this template does:
    Code:
    template <size_t n>
    size_t StringSize(char (&CharArray)[n]) {return n-1;} // returns the size of an array of characters minus the null terminator.
    Last edited by SuperKoko; August 5th, 2005 at 05:25 AM.

  7. #7
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: what is the diff between char str[] and char * str ?

    I just discovered this thread you should read.
    It contains more informations on argument arrays.

  8. #8
    Join Date
    Aug 2005
    Posts
    12

    Re: what is the diff between char str[] and char * str ?

    well the most difference is the obvios one:
    char str[] is an chararray ( memory allocation)
    while char * str is an pointer to an memorydestination( doesn#t allocate
    memory) ;-)

  9. #9
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457

    Re: what is the diff between char str[] and char * str ?

    Quote Originally Posted by rene1
    well the most difference is the obvios one:
    char str[] is an chararray ( memory allocation)
    while char * str is an pointer to an memorydestination( doesn#t allocate
    memory) ;-)
    Ummm...
    Code:
    char str1[] = "str1";
    char* str2 = "str2";
    where does "str2" get stored then, if not in memory?
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  10. #10
    Join Date
    Jan 2001
    Posts
    588

    Re: what is the diff between char str[] and char * str ?

    Quote Originally Posted by Bond
    Ummm...
    Code:
    char str1[] = "str1";
    char* str2 = "str2";
    where does "str2" get stored then, if not in memory?
    The two are slightly different. The nuance is this:

    Code:
    char str1[] = "str1";
    This would declare an array large enough to hold the string literal used as the rvalue. This array is initialized with the string's value. Therefore, there is a 4-byte array declared here containing 's', 't', 'r', and '\0'. This array has read/write access and can be modified without any issue. str1 contains the address of the first character.

    Code:
    char *str2 = "str2";
    In this case, we are declaring a pointer to a character. This pointer is initialized to point to the beginning of a string literal. This literal may be stored in a read-only section of the program. I wasn't actually aware that this would compile without a warning, as you're initializing a non-const pointer to point to a const string literal. str2 points to the first character of the string literal, which may or may not actually be writable.

  11. #11
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715

    Re: what is the diff between char str[] and char * str ?

    Code:
    char *ptr   =  "zipy zippy zoo";
    char ptr2[] = "blah blah blah";
    char ptr3[15] = "zip zip bip";
    
    foo ( char *myptr)
    {
        printf( "%s\n", myptr );
    }
    
    int main()
    {
       char *ptr4 = new[15];
       strcpy( ptr4, "shazam");
    
        foo(ptr);
        foo(ptr2);
        foo(ptr3)
        foo(ptr4);
    
       delete []ptr4;
    }
    fact is any array of anything is just a pointer. The array syntax just allows you the functionality to associate memory with that pointer. Since they're both pointers the syntax for addressing them is interchangable.

    Code:
    typedef struct MYSTRUCT
    {
         int  i;
         char c;
         char z[20];
    }  MYSTRUCT, *PMYSTRUCT;
    
    int main()
    {
          MYSTRUCT     myArray[10];
          PMYSTRUCT   pMyArray = new MYSTRUCT[10];
    
          myArray->i   = 1;      // set's the first value of the array
          pMyArray->i = 1;      // set's the first value of the pointer memory
    
          (myArray+2)->i   = 1;      // set's the third item of the array
          (pMyArray+2)->i = 1;      // set's the third item of the pointed to memory
    
          myArray[2].i   = 3;    // set's the third item of the array
          pMyArray[2].i = 3;    // set's the third item of the pointed to memory
    
          delete []pMyArray;
    }
    base[] == *base derefference
    base[#] == *(base+offset) add an offset to the base and dereffrence





    arrays are just pointers with memory already associated with them. Pointers are just arrays which may or may not have the memory associated with them.

    The only difference I know of is one which SuperKoko touched upon..

    You can't change the base address of an array. Pointers you can..

    char szbuffer[10] = "";
    char *pszBuffer = new char[10];

    szBuffer += 1; // ilegal
    pszBuffer += 1; // legal


    An array contains an address, and a pointer contains an address.
    The rest is just syntax.
    Last edited by JMS; August 5th, 2005 at 03:01 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured