The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to replace any char with newline char. mightysam Shell Programming and Scripting 5 09-18-2008 08:15 PM
char c = 882 useless79 High Level Programming 1 07-30-2007 05:16 AM
Regarding char Pointer sweta High Level Programming 2 06-04-2006 12:09 AM
Convert Char to Dec using AWK jasmine05 Shell Programming and Scripting 4 06-09-2005 03:50 AM
\n char in C C|[anti-trust] High Level Programming 1 05-05-2005 06:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-19-2006
arunviswanath arunviswanath is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 8
char *p and char p[].

Can anyone please explain me the difference between char *p and char p[] ?

Thanks in Advance,
Arun.
  #2 (permalink)  
Old 07-19-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
The difference is that 'char *p' is a pointer while 'char p[n]' is an array. So char *p will be allocated enough space to hold a memory address that will contain a character type variable, while p[n] will be allocated n*sizeof(character type) contiguous memory locations where the data will reside.

If you allocate space using 'char p[10]', then using something like
Code:
printf("%c",p[0]);
or
Code:
printf("%c",*p);
will give the same output.
  #3 (permalink)  
Old 07-19-2006
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
there can be numerous differences, depends on what context you want to use this in. The basic difference is as pointed above by blowtorch.
  #4 (permalink)  
Old 07-19-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,928
Considering that arrays are also pointers, there's two main differences.
  • Arrays are allocated space. The only reason function arguments get away with leaving the [] empty is they are passed a pointer, which the compiler assumes to have been allocated.
  • Arrays are constant. You can't change their base address.
Other than that, pointers and arrays are nearly identical.
  #5 (permalink)  
Old 07-20-2006
Hitori's Avatar
Hitori Hitori is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2006
Posts: 360
You can think about arrays as constant pointers on automatically allocated memory that cannot be released
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 07:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0