Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 5th, 2008, 3:41 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Apr 5th, 2008, 3:53 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
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.
Narue is offline   Reply With Quote
Old Apr 5th, 2008, 4:42 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Apr 5th, 2008, 8:21 PM   #4
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 547
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr.
Ancient Dragon is offline   Reply With Quote
Old Apr 6th, 2008, 12:30 AM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Apr 6th, 2008, 1:40 AM   #6
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 272
Rep Power: 2 Jabo is on a distinguished road
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!
Jabo is offline   Reply With Quote
Old Apr 6th, 2008, 1:43 AM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: char[] vs char*

Quote:
Originally Posted by titaniumdecoy View Post
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.
From the 1999 C standard, Section 5.1.2.2.1, clause 2, fifth bullet point "The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination."

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
grumpy is offline   Reply With Quote
Old Apr 6th, 2008, 1:59 AM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: char[] vs char*

Thanks, grumpy. That is quite an interesting tidbit.
titaniumdecoy is offline   Reply With Quote
Old Apr 13th, 2008, 10:30 PM   #9
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: char[] vs char*

Quote:
Originally Posted by Narue View Post
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.
Does anyone know of an official source where I can find this information? (A link would be appreciated.) Thanks.
titaniumdecoy is offline   Reply With Quote
Old Apr 14th, 2008, 4:31 AM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: char[] vs char*

The C standard is the official source. You can buy a copy through the ANSI web store.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:47 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC