PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > difference between a pointer array and a stack array?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
difference between a pointer array and a stack array?

Réponse
 
LinkBack Outils de la discussion
Vieux 25/05/2008, 20h37   #1
Ahmad Humayun
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut difference between a pointer array and a stack array?

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
  Réponse avec citation
Vieux 25/05/2008, 21h02   #2
Jens Thoms Toerring
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference between a pointer array and a stack array?

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
  Réponse avec citation
Vieux 25/05/2008, 21h04   #3
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference between a pointer array and a stack array?

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"
  Réponse avec citation
Vieux 26/05/2008, 20h26   #4
Ahmad Humayun
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference between a pointer array and a stack array?

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
  Réponse avec citation
Vieux 26/05/2008, 20h39   #5
Ahmad Humayun
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference between a pointer array and a stack array?

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
  Réponse avec citation
Vieux 27/05/2008, 14h20   #6
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference between a pointer array and a stack array?

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;
}
  Réponse avec citation
Vieux 28/05/2008, 02h58   #7
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference between a pointer array and a stack array?

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
  Réponse avec citation
Vieux 28/05/2008, 18h00   #8
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference between a pointer array and a stack array?

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/>
  Réponse avec citation
Vieux 28/05/2008, 18h11   #9
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference between a pointer array and a stack array?

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"
  Réponse avec citation
Vieux 28/05/2008, 18h20   #10
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference between a pointer array and a stack array?

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.
  Réponse avec citation
Vieux 28/05/2008, 19h04   #11
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference between a pointer array and a stack array?

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"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h21.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23922 seconds with 19 queries