CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #10
    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