By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,952 Members | 1,411 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,952 IT Pros & Developers. It's quick & easy.

difference in array and pointer

P: n/a
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 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ajay.bansa...@gmail.com 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.

Nov 22 '05 #2

P: n/a
aj***********@gmail.com 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
Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.