|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Whats the difference between:
char str1[] = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2 (isn't str1 technically a pointer?) Thanks |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Ahmad Humayun <ahmad.humyn@gmail.com> wrote:
> Whats the difference between: > char str1[] = "wxyz"; > char* str2 = "abcd"; > I can do this: > str2 = str1 > but I can't do this: > str1 = str2 > (isn't str1 technically a pointer?) No. 'str1' is an array of 5 chars, initialized to the characters 'w', 'x', 'y', 'z' and '\0'. Only if 'str1' is used in a place where a value is required (e.g. if 'str1' is an argument of a function call) it gets replaced automatically by a pointer to the first element of that array. But that doesn't change any- thing about the "nonpointerness" of 'str1', it's an array and remains to be an array until it goes out of scope. In contrast, 'str2' is a pointer, initialized to point to the string literal "abcd" (that could very well be in read-only memory). Since 'str2' is a pointer you can assign it a different value, e.g. by using str2 = str1; This works because in this case on the right hand side of the asignment a value is required and now "the rule" applies, i.e. that if an array is used in a context where a value is needed it is replaced by a pointer to its first element. This automatic conversion is actually not much different from what happens when you write int a = 1.2; Since on the right hand side an int value is required the double value you have there is automatically converted to an int value. C could in principle refrain from doing such automatic conversions and require that you explicitely state your intent like int a = ( int ) 1.2; or str2 = &str[ 0 ]; but that's not how the inventors of C decided to do it and in- stead introduced some automatic conversions. But str1 = str2; is still a syntax error since 'str1' is not a pointer and can't be treated like a pointer because it has a completely different type. Regards, Jens -- \ Jens Thoms Toerring ___ jt@toerring.de \__________________________ http://toerring.de |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Ahmad Humayun <ahmad.humyn@gmail.com> writes:
> Whats the difference between: > > char str1[] = "wxyz"; > char* str2 = "abcd"; str1 is an array; str2 is a pointer. > I can do this: > str2 = str1 Right. str1, an array expression, is implicitly converted to a pointer in most contexts, including this one. > but I can't do this: > str1 = str2 Right, you can't assign to an array. > (isn't str1 technically a pointer?) No, str1 is an array. Arrays are not pointers; pointers are not arrays. This is probably the most common misconception about C. Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>. Feel free to post again with more specific questions if you're still confused after that. -- 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" |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On May 26, 1:04am, Keith Thompson <ks...@mib.org> wrote:
> Ahmad Humayun <ahmad.hu...@gmail.com> writes: > > Whats the difference between: > > > char str1[] = "wxyz"; > > char* str2 = "abcd"; > > str1 is an array; str2 is a pointer. > > > I can do this: > > str2 = str1 > > Right. str1, an array expression, is implicitly converted to a > pointer in most contexts, including this one. > > > but I can't do this: > > str1 = str2 > > Right, you can't assign to an array. > > > (isn't str1 technically a pointer?) > > No, str1 is an array. Arrays are not pointers; pointers are not > arrays. This is probably the most common misconception about C. > > Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>. Feel > free to post again with more specific questions if you're still > confused after that. > > -- > Keith Thompson (The_Other_Keith) ks...@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" Thanks Jens and Antony....this info was really really ful ![]() Happy coding ![]() |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On May 26, 1:04am, Keith Thompson <ks...@mib.org> wrote:
> Ahmad Humayun <ahmad.hu...@gmail.com> writes: > > Whats the difference between: > > > char str1[] = "wxyz"; > > char* str2 = "abcd"; > > str1 is an array; str2 is a pointer. > > > I can do this: > > str2 = str1 > > Right. str1, an array expression, is implicitly converted to a > pointer in most contexts, including this one. > > > but I can't do this: > > str1 = str2 > > Right, you can't assign to an array. > > > (isn't str1 technically a pointer?) > > No, str1 is an array. Arrays are not pointers; pointers are not > arrays. This is probably the most common misconception about C. > > Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>. Feel > free to post again with more specific questions if you're still > confused after that. > > -- > Keith Thompson (The_Other_Keith) ks...@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" Thanks Jens and Antony....this info was really really ful ![]() Happy coding ![]() |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On May 25, 10:37 pm, Ahmad Humayun <ahmad.hu...@gmail.com> wrote:
> Whats the difference between: > > char str1[] = "wxyz"; > char* str2 = "abcd"; > > I can do this: > str2 = str1 > > but I can't do this: > str1 = str2 > > (isn't str1 technically a pointer?) str1 would be a pointer only in function declarations and definitions: int foo(char str1[], char *str2) { str1 = str2; /* valid */ return 0; } |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
4On Tue, 27 May 2008 06:20:12 -0700 (PDT), vippstar@gmail.com wrote:
>On May 25, 10:37 pm, Ahmad Humayun <ahmad.hu...@gmail.com> wrote: >> Whats the difference between: >> >> char str1[] = "wxyz"; >> char* str2 = "abcd"; >> >> I can do this: >> str2 = str1 >> >> but I can't do this: >> str1 = str2 >> >> (isn't str1 technically a pointer?) No, str1 is technically an array. >str1 would be a pointer only in function declarations and definitions: str1 is never a pointer but it is converted to a pointer in many cases, not just the two you mention. When used in an expression, str1 has type array of 5 char. As such, this expression will be automatically converted by the compiler to a pointer to the first element of the array with type pointer to char (effectively &str1[0]) in every case except: when used as the operand of the sizeof operator when used as the operand of the & operator (There is a third exception but it applies only to string literals used to initialize an array as part of the array definition.) This is why the statement str2 = str1; is legal. str2 has type char*. The expression str1 is converted to an expression that has type char*. It is legal to assign an expression of type char* to an object of type char*. > >int foo(char str1[], char *str2) { > str1 = str2; /* valid */ > return 0; >} Remove del for email |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On May 28, 4:58 am, Barry Schwarz <schwa...@dqel.com> wrote:
> 4On Tue, 27 May 2008 06:20:12 -0700 (PDT), vipps...@gmail.com wrote: > > >On May 25, 10:37 pm, Ahmad Humayun <ahmad.hu...@gmail.com> wrote: > >> Whats the difference between: > > >> char str1[] = "wxyz"; > >> char* str2 = "abcd"; > > >> I can do this: > >> str2 = str1 > > >> but I can't do this: > >> str1 = str2 > > >> (isn't str1 technically a pointer?) > > No, str1 is technically an array. > > >str1 would be a pointer only in function declarations and definitions: > > str1 is never a pointer but it is converted to a pointer in many > cases, not just the two you mention. str1 in my example code is a pointer. > When used in an expression, str1 has type array of 5 char. As such, > this expression will be automatically converted by the compiler to a > pointer to the first element of the array with type pointer to char > (effectively &str1[0]) in every case except: > when used as the operand of the sizeof operator > when used as the operand of the & operator > > (There is a third exception but it applies only to string literals > used to initialize an array as part of the array definition.) > > This is why the statement str2 = str1; is legal. str2 has type char*. > The expression str1 is converted to an expression that has type char*. No, str1 is a char * (in my example). > It is legal to assign an expression of type char* to an object of type > char*. > > > > >int foo(char str1[], char *str2) { > > str1 = str2; /* valid */ > > return 0; > >} See question 6.4 of the c-faq. <http://c-faq.com/> |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
vippstar@gmail.com writes:
> On May 28, 4:58 am, Barry Schwarz <schwa...@dqel.com> wrote: >> 4On Tue, 27 May 2008 06:20:12 -0700 (PDT), vipps...@gmail.com wrote: >> >> >On May 25, 10:37 pm, Ahmad Humayun <ahmad.hu...@gmail.com> wrote: >> >> Whats the difference between: >> >> >> char str1[] = "wxyz"; >> >> char* str2 = "abcd"; >> >> >> I can do this: >> >> str2 = str1 >> >> >> but I can't do this: >> >> str1 = str2 >> >> >> (isn't str1 technically a pointer?) >> >> No, str1 is technically an array. >> >> >str1 would be a pointer only in function declarations and definitions: >> >> str1 is never a pointer but it is converted to a pointer in many >> cases, not just the two you mention. > str1 in my example code is a pointer. str1, in the only example code quoted here, is an array, not a pointer. [...] >> This is why the statement str2 = str1; is legal. str2 has type char*. >> The expression str1 is converted to an expression that has type char*. > No, str1 is a char * (in my example). Then perhaps your example got lost. The sample code, if the attributions are correct was originally posted by Ahmad Humayun; in that code, str1 is declared as an array. [snip] -- 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" |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
On May 28, 8:11 pm, Keith Thompson <ks...@mib.org> wrote:
> vipps...@gmail.com writes: > > On May 28, 4:58 am, Barry Schwarz <schwa...@dqel.com> wrote: > >> 4On Tue, 27 May 2008 06:20:12 -0700 (PDT), vipps...@gmail.com wrote: > > >> >On May 25, 10:37 pm, Ahmad Humayun <ahmad.hu...@gmail.com> wrote: > >> >> Whats the difference between: > > >> >> char str1[] = "wxyz"; > >> >> char* str2 = "abcd"; > > >> >> I can do this: > >> >> str2 = str1 > > >> >> but I can't do this: > >> >> str1 = str2 > > >> >> (isn't str1 technically a pointer?) > > >> No, str1 is technically an array. > > >> >str1 would be a pointer only in function declarations and definitions: > > >> str1 is never a pointer but it is converted to a pointer in many > >> cases, not just the two you mention. > > str1 in my example code is a pointer. > > str1, in the only example code quoted here, is an array, not a > pointer. > > [...] > > >> This is why the statement str2 = str1; is legal. str2 has type char*. > >> The expression str1 is converted to an expression that has type char*. > > No, str1 is a char * (in my example). > > Then perhaps your example got lost. The sample code, if the > attributions are correct was originally posted by Ahmad Humayun; in > that code, str1 is declared as an array. > > [snip] My example was right in that [snip]. Let's follow the discussion, first my message: -- message -- On May 25, 10:37 pm, Ahmad Humayun <ahmad.hu...@gmail.com> wrote: > Whats the difference between: > char str1[] = "wxyz"; > char* str2 = "abcd"; > I can do this: > str2 = str1 > but I can't do this: > str1 = str2 > (isn't str1 technically a pointer?) str1 would be a pointer only in function declarations and definitions: int foo(char str1[], char *str2) { <------ my example str1 = str2; /* valid */ return 0; } -- message -- Then Mr Schwarz reply: -- message -- 4On Tue, 27 May 2008 06:20:12 -0700 (PDT), vipps...@gmail.com wrote: >On May 25, 10:37 pm, Ahmad Humayun <ahmad.hu...@gmail.com> wrote: >> Whats the difference between: >> char str1[] = "wxyz"; >> char* str2 = "abcd"; >> I can do this: >> str2 = str1 >> but I can't do this: >> str1 = str2 >> (isn't str1 technically a pointer?) No, str1 is technically an array. >str1 would be a pointer only in function declarations and definitions: str1 is never a pointer but it is converted to a pointer in many cases, not just the two you mention. When used in an expression, str1 has type array of 5 char. As such, this expression will be automatically converted by the compiler to a pointer to the first element of the array with type pointer to char (effectively &str1[0]) in every case except: when used as the operand of the sizeof operator when used as the operand of the & operator (There is a third exception but it applies only to string literals used to initialize an array as part of the array definition.) This is why the statement str2 = str1; is legal. str2 has type char*. The expression str1 is converted to an expression that has type char*. It is legal to assign an expression of type char* to an object of type char*. >int foo(char str1[], char *str2) { /* <---- my code here again */ > str1 = str2; /* valid */ > return 0; >} Remove del for email -- message -- I think it's clear that when I said this: > str1 would be a pointer only in function declarations and definitions: > <snip code> I was talking about my code, and not Mr Humayuns code. Then Mr Schwarz says: > >str1 would be a pointer only in function declarations and definitions: > str1 is never a pointer but it is converted to a pointer in many > cases, not just the two you mention. Mr Schwarz either took that out of context or he was not aware that in my example, indeed, str1 is a pointer. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
vippstar@gmail.com writes:
> On May 28, 8:11 pm, Keith Thompson <ks...@mib.org> wrote: [...] >> str1, in the only example code quoted here, is an array, not a >> pointer. >> >> [...] >> >> >> This is why the statement str2 = str1; is legal. str2 has type char*. >> >> The expression str1 is converted to an expression that has type char*. >> > No, str1 is a char * (in my example). >> >> Then perhaps your example got lost. The sample code, if the >> attributions are correct was originally posted by Ahmad Humayun; in >> that code, str1 is declared as an array. >> >> [snip] > My example was right in that [snip]. [...] So it was. The article to which I replied did have a declaration of str1 (as an array), and I missed the other declaration that appeared at the bottom of that article, after your statement that you had declared it as a pointer. Incidentally, the original declaration: char str1[] = "wxyz"; cannot be a pointer declaration; for it to be one, you'd have to move it into a function prototype *and* drop the `` = "wxyz";''. -- 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" |
|
![]() |
| Outils de la discussion | |
|
|