![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
char[] vs char*
I am confused as to the difference between a char array and a char pointer in C. For example:
char a[] = "one"; char *b = "two" As I understand it, both are essentially pointers to the first character of an array except a can be modified and b cannot. Why is this? If I then write char *c = a;, can c be used to modify the character array a?Compare the following two functions declarations: void f1(char x[]); void f2(char *x); If I pass a char[] to f2, can I modify it, or only if I pass it to f1? I assume it would be incorrect to pass a char* to f1--is this true? Finally, compare the following two declarations for main: int main(int argc, char *argv[]); int main(int argc, char **argv); Does the latter indicate that the contents of argv can be modified? Or are the two declarations identical? Thanks for any help clearing this up. |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
Re: char[] vs char*
>As I understand it, both are essentially pointers to the first character
>of an array except a can be modified and b cannot. Nope, and that's the problematic misconception. An array is not a pointer, it's an array. In many cases an array will be converted to a pointer automagically for you, but that still doesn't make it a pointer. >Why is this? When you initialize an array with a string literal, a copy of the string literal is stored in the array. You own the memory for the array, therefore you can modify the copy of the string literal (provided it's not declared as const). When you initialize a pointer with a string literal, the pointer points to the string literal itself. The reason you can't use b to modify the string literal is because string literals are read-only. >I assume it would be incorrect to pass a char* to f1--is this true? No, in the case of function parameters, a pointer and an array are identical. The array notation is simply syntactic sugar.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
Re: char[] vs char*
I'm not sure if this helps, but I only use the array notation when I'm hardcoding an array of values, or a string, and thus initiating it on the same line I declare it. The sad thing is I can't explain why I use this habit. The explanation can probably be found somewhere in Narue's post. I've only noticed it's what people do, and it's what has worked for me.
|
|
|
|
|
|
#4 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 532
Rep Power: 4
![]() |
Re: char[] vs char*
when NOT used as a parameter to a function always use the array notation if you think you will change any of the characters in the array.
A pointer probably points to somewhere in read-only memory so it can not be changed. The pointer notation should really be made const so that you can't change the string to which it points.
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#5 |
|
Expert Programmer
|
Re: char[] vs char*
Thanks, everyone. That clears up most of my questions. But I am still wondering whether the char arrays/pointers passed to main (argv) can be modified legally.
|
|
|
|
|
|
#6 |
|
Not a user?
Join Date: Sep 2007
Posts: 256
Rep Power: 2
![]() |
Re: char[] vs char*
can you call main from within main? probably not. to change the arguments, you would have to call the function again, or so I'm thinking, and it's highly unlikely that they would allow main to be called recursively.
As it turns out, it looks like C allows main to be called recursively, but C++ doesn't I stand corrected Last edited by Jabo; Apr 6th, 2008 at 1:45 AM. Reason: I stand corrected! |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
Re: char[] vs char*
Quote:
The C++ standard says nothing but, since its second argument (aka argv) has no const modifiers, it may be inferred it is legal to modify the strings passed to main via argv.
__________________
Dear God So far today I've done all right. I haven't been grumpy yet. But in a few minutes, God, I'm going to get out of bed, and from then on I'm going to need a lot more help. AMEN |
|
|
|
|
|
|
#8 |
|
Expert Programmer
|
Re: char[] vs char*
Thanks, grumpy. That is quite an interesting tidbit.
|
|
|
|
|
|
#9 | |
|
Expert Programmer
|
Re: char[] vs char*
Quote:
|
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
Re: char[] vs char*
The C standard is the official source. You can buy a copy through the ANSI web store.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 25 ways to efficiently sort an array/list of integers | Jimbo | Software Design and Algorithms | 36 | Mar 3rd, 2008 6:31 AM |
| 500 Ways to Program Numbers 1 Through 10! | Sane | Coder's Corner Lounge | 637 | Jan 5th, 2008 10:15 PM |
| char * to char[] | titaniumdecoy | C++ | 6 | Jul 28th, 2006 12:53 PM |
| char[] problem | Kilo | C++ | 13 | Jun 19th, 2006 2:18 AM |
| Convert char[] to int | King | C++ | 18 | Jan 31st, 2006 3:55 PM |