char *p and char p[].


 
Thread Tools Search this Thread
Top Forums Programming char *p and char p[].
# 1  
Old 07-19-2006
char *p and char p[].

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

Thanks in Advance,
Arun.
# 2  
Old 07-19-2006
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  
Old 07-19-2006
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  
Old 07-19-2006
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  
Old 07-20-2006
You can think about arrays as constant pointers on automatically allocated memory that cannot be released
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

2. Shell Programming and Scripting

Counting a Char

how to count how many R in the below file. Rttt 1Rxxx GG 2R 3R 4R 5R 6R 7R R0 R1 R2 R3 R4 R5 R6 R7 RR (10 Replies)
Discussion started by: theshashi
10 Replies

3. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

4. Shell Programming and Scripting

how first char in odd line and second char in even line

Hi I m having ifconfig -a o/p like sbanlab1:ksh# ifconfig -a | egrep "flags|inet" | awk -F' ' '{print $1,$2}' lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 bge0:... (1 Reply)
Discussion started by: tarunn.dubeyy
1 Replies

5. UNIX for Dummies Questions & Answers

wild char * in if

if ; then => is it correct? i need to check 10 files size and do the same action. The file name is same but extension of the files are different. how do i deal with it? Please help (1 Reply)
Discussion started by: nidhink
1 Replies

6. Programming

Char initialization

Hi All, char a="\0"; a) a contains \0 a contains garbage value b) a contains \ a contains 0 a contains garbage value Pls, let me know correct result is a or b. I guess a. Thanks, Naga:cool: (2 Replies)
Discussion started by: Nagapandi
2 Replies

7. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

8. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

9. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

10. Programming

\n char in C

how can i change line in the file while using write(desc,&c,sizeof(char)); ? (1 Reply)
Discussion started by: C|[anti-trust]
1 Replies
Login or Register to Ask a Question