Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > difference in array and pointer

Reply
Thread Tools

difference in array and pointer

 
 
ajay.bansal02@gmail.com
Guest
Posts: n/a
 
      11-16-2005
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

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      11-16-2005
(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      11-16-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
difference between a pointer array and a stack array? Ahmad Humayun C Programming 10 05-28-2008 06:04 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments