|  | String function problem |  | |
| | | Vijay |  |
| Posted: Tue Jun 10, 2008 6:11 am Post subject: String function problem |  |
Hello everybody, I am new to the C language can anybody tell me the diffrence between char nm[10] and char *nm ? |
| |
| | | Keith Thompson |  |
| Posted: Tue Jun 10, 2008 6:11 am Post subject: Re: String function problem |  |
Vijay <k.anurag87@yahoo.com> writes:
| Quote: | Hello everybody, I am new to the C language can anybody tell me the diffrence between char nm[10] and char *nm ?
|
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Section 6 covers arrays and pointers.
-- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
| |
| | | Walter Roberson |  |
| Posted: Tue Jun 10, 2008 6:29 am Post subject: Re: String function problem |  |
| |  | |
In article <f9e301c2-880a-44f2-a537-e20cfa602dfc@p39g2000prm.googlegroups.com>, Vijay <k.anurag87@yahoo.com> wrote:
| Quote: | Hello everybody, I am new to the C language can anybody tell me the diffrence between char nm[10] and char *nm ?
|
char nm[10]
declares that nm is a variable in which 10 char can be stored. Space for those 10 characters will be automatically allocated. You can "really" store characters in the array nm.
char *nm
declares that nm is a variable which can be set to point to a character and then used to access characters pointed to. But the only space allocated by the statement is space for the pointer itself, and that pointer is not initialized to anything in particular, so until you cause nm to point to some real storage, this nm cannot be used to access anything.
The above analysis does not apply if these are found in parameter lists in function declarations: in parameter lists, there is no difference in meaning, with both meaning that nm is of type pointer to char. -- "There's no term to the work of a scientist." -- Walter Reisch |
| |
| | | pete |  |
| Posted: Tue Jun 10, 2008 6:39 am Post subject: Re: String function problem |  |
rahul wrote:
| Quote: | Array name without any indexes is the pointer to the first element of the array.
|
There's three exceptions to that:
N869 6.3.2 Other operands 6.3.2.1 Lvalues and function designators
[#3] Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ``array of type'' is converted to an expression with type ``pointer to type'' that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
-- pete |
| |
| | | rahul |  |
| Posted: Tue Jun 10, 2008 7:46 am Post subject: Re: String function problem |  |
On Jun 10, 11:11 am, Vijay <k.anura...@yahoo.com> wrote:
| Quote: | Hello everybody, I am new to the C language can anybody tell me the diffrence between char nm[10] and char *nm ?
|
char foo[10] defines an array of 10 characters. char *bar defines bar as a pointer to a character. Array name without any indexes is the pointer to the first element of the array. foo points to the first element i.e foo == &foo[0]. When passed to a function, the array decays to pointer to first element. bar can be dynamically allocated memory to contain more than one characters. The difference between foo and bar is foo is address of linearly allocated memory while bar, after mem allocation, contains the address of linearly allocated memory. |
| |
| | | Jens Thoms Toerring |  |
| Posted: Tue Jun 10, 2008 8:57 am Post subject: Re: String function problem |  |
| |  | |
rahul <rahulsinner@gmail.com> wrote:
| Quote: | On Jun 10, 11:11 am, Vijay <k.anura...@yahoo.com> wrote: Hello everybody, I am new to the C language can anybody tell me the diffrence between char nm[10] and char *nm ?
char foo[10] defines an array of 10 characters. char *bar defines bar as a pointer to a character.
Array name without any indexes is the pointer to the first element of the array.
|
Not really, an unadored array name stands for (not is) a pointer to the first element of the array in a context where a value is required. For example in
int a[10]; int *b = malloc( 100 );
a = b;
'a' is not a pointer. First reason is that 'a' appears not in a position where a value is allowed. Here you need something you can assign a value to. Second, if 'a' would be a pointer the assignment would be correct (you can assign a new value to a pointer) and you could actually change 'a' by assigning it a new value and suddenly '*a' would be the first value in the memory 'b' points to while 'a[0]' would still be the value of the first element of the array. Only someone with a strong interest in the obfuscated C contest (or a masochist) would truely enjoy that, I guess;-)
Regards, Jens -- \ Jens Thoms Toerring ___ jt@toerring.de \__________________________ LINK |
| |
| | | pete |  |
| Posted: Tue Jun 10, 2008 1:56 pm Post subject: Re: String function problem |  |
| |  | |
Jens Thoms Toerring wrote:
| Quote: | rahul <rahulsinner@gmail.com> wrote: On Jun 10, 11:11 am, Vijay <k.anura...@yahoo.com> wrote: Hello everybody, I am new to the C language can anybody tell me the diffrence between char nm[10] and char *nm ?
char foo[10] defines an array of 10 characters. char *bar defines bar as a pointer to a character.
Array name without any indexes is the pointer to the first element of the array.
Not really, an unadored array name stands for (not is) a pointer to the first element of the array in a context where a value is required.
|
That's not the rule. The implicit conversion is specified as taking place always, with three exceptions.
| Quote: | For example in
int a[10]; int *b = malloc( 100 );
a = b;
'a' is not a pointer.
|
(a) is converted to a pointer type in that context.
| Quote: | First reason is that 'a' appears not in a position where a value is allowed. Here you need something you can assign a value to. Second, if 'a' would be a pointer the assignment would be correct (you can assign a new value to a pointer) and you could actually change 'a' by assigning it a new value and suddenly '*a' would be the first value in the memory 'b' points to while 'a[0]' would still be the value of the first element of the array.
|
That's wrong. The standard explicitly states that an array type is not a "modifiable lvalue", however, even if it didn't: The conversion of the array type expression to a pointer type, makes the resulting expression not an lvalue, which is sufficient to disallow an array type from appearing as a left assignment operand.
6.3.2 Other operands 6.3.2.1 Lvalues and function designators
[#3] Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ``array of type'' is converted to an expression with type ``pointer to type'' that points to the initial element of the array object and is not an lvalue.
6.5.16 Assignment operators
[#2] An assignment operator shall have a modifiable lvalue as its left operand.
-- pete |
| |
|
|