The UNIX and Linux Forums  

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 3 Weeks Ago 05:15 PM
char c = 882 useless79 High Level Programming 1 07-30-2007 02:16 AM
Regarding char Pointer sweta High Level Programming 2 06-03-2006 09:09 PM
Convert Char to Dec using AWK jasmine05 Shell Programming and Scripting 4 06-09-2005 12:50 AM
\n char in C C|[anti-trust] High Level Programming 1 05-05-2005 03:15 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-19-2006
Registered User
 

Join Date: Jul 2006
Posts: 8
Stumble this Post!
char *p and char p[].

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

Thanks in Advance,
Arun.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-19-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,325
Stumble this Post!
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.
Reply With Quote
  #3 (permalink)  
Old 07-19-2006
linuxpenguin's Avatar
Registered User
 

Join Date: May 2002
Location: India
Posts: 295
Stumble this Post!
there can be numerous differences, depends on what context you want to use this in. The basic difference is as pointed above by blowtorch.
__________________
War doesnt determine who is right, it determines who is left
Reply With Quote
  #4 (permalink)  
Old 07-19-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 923
Stumble this Post!
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.
Reply With Quote
  #5 (permalink)  
Old 07-19-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 356
Stumble this Post!
You can think about arrays as constant pointers on automatically allocated memory that cannot be released
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0