difference in array and pointer

A

ajay.bansal02

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
 
N

Neelesh Bodas

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.
 
J

John Harrison

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.

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

john
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top