Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   char[] vs char* (http://www.programmingforums.org/showthread.php?t=15557)

titaniumdecoy Apr 5th, 2008 4:41 PM

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. :icon_confused:

Narue Apr 5th, 2008 4:53 PM

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.

Sane Apr 5th, 2008 5:42 PM

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.

Ancient Dragon Apr 5th, 2008 9:21 PM

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.

titaniumdecoy Apr 6th, 2008 1:30 AM

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.

Jabo Apr 6th, 2008 2:40 AM

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

grumpy Apr 6th, 2008 2:43 AM

Re: char[] vs char*
 
Quote:

Originally Posted by titaniumdecoy (Post 143539)
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.

titaniumdecoy Apr 6th, 2008 2:59 AM

Re: char[] vs char*
 
Thanks, grumpy. That is quite an interesting tidbit.

titaniumdecoy Apr 13th, 2008 11:30 PM

Re: char[] vs char*
 
Quote:

Originally Posted by Narue (Post 143522)
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.

grumpy Apr 14th, 2008 5:31 AM

Re: char[] vs char*
 
The C standard is the official source. You can buy a copy through the ANSI web store.


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

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