difference in array and pointer

Discussion in 'C++' started by ajay.bansal02@gmail.com, Nov 16, 2005.

  1. Guest

    Hi All

    What is difference in

    TCHAR myVar[]=.....;
    and
    TCHAR *myVAR1=(TCHAR *)malloc....;

    arent both myVar and myVAR1are same?

    what difference would it make if i typecast myVar as (TCHAR *)myVar?

    -Ajay
    , Nov 16, 2005
    #1
    1. Advertising

  2. wrote:
    > Hi All
    >
    > What is difference in
    >
    > TCHAR myVar[]=.....;


    myVar is an array of TCHARs. Thus type of myVar is TCHAR[]

    > and
    > TCHAR *myVAR1=(TCHAR *)malloc....;

    myVAR1 is a pointer to a TCHAR. Thus type of myVAR1 is TCHAR*


    >
    > arent both myVar and myVAR1are same?


    TCHAR is a name of an array, TCHAR* is a pointer. The two are not the
    same.
    (However, when passed to a function, array name implicitly gets
    converted to the pointer to the first element)

    > what difference would it make if i typecast myVar as (TCHAR *)myVar?


    the result will represent a pointer to TCHAR

    Except for address of and sizeof operators, an array name implicitly
    gets converted to the pointer to the first element, when passed to a
    function or operator.

    Hope this helps.
    Neelesh Bodas, Nov 16, 2005
    #2
    1. Advertising

  3. wrote:
    > Hi All
    >
    > What is difference in
    >
    > TCHAR myVar[]=.....;
    > and
    > TCHAR *myVAR1=(TCHAR *)malloc....;
    >
    > arent both myVar and myVAR1are same?


    No, arrays and pointers are not the same. I many cases you can use them
    the same way, but that does not make them the same.

    Here's one difference

    myVAR1 = myVar; // legal, myVAR1 now points to the start of the array

    myVar = myVAR1;// illegal, doesn't compile

    The other big differences are that you have to to remember to free the
    myVAR1 memory using free, and that you can allocate different sized
    memory blocks using malloc, but an array is a fixed size.

    >
    > what difference would it make if i typecast myVar as (TCHAR *)myVar?


    Very little.

    >
    > -Ajay
    >


    Suggest you read a book on C or C++.

    john
    John Harrison, Nov 16, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    424
    Keith Thompson
    Oct 3, 2006
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    674
    Martin Ambuhl
    Jan 28, 2008
  3. Ahmad Humayun
    Replies:
    10
    Views:
    552
    Keith Thompson
    May 28, 2008
  4. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    451
    James Kuyper
    Sep 23, 2011
  5. somenath
    Replies:
    10
    Views:
    279
    James Kanze
    Jul 2, 2013
Loading...

Share This Page