difference between a pointer array and a stack array?

A

Ahmad Humayun

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
 
J

Jens Thoms Toerring

Ahmad Humayun said:
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
 
K

Keith Thompson

Ahmad Humayun said:
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.
 
A

Ahmad Humayun

Ahmad Humayun said:
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) (e-mail address removed)  <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 helpful :)

Happy coding :)
 
A

Ahmad Humayun

Ahmad Humayun said:
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) (e-mail address removed)  <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 helpful :)

Happy coding :)
 
V

vippstar

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;
}
 
B

Barry Schwarz

4On said:
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
 
V

vippstar

4On said:
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/>
 
K

Keith Thompson

4On said:
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.

[...]
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]
 
V

vippstar

4On Tue, 27 May 2008 06:20:12 -0700 (PDT), (e-mail address removed) 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.

[...]
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 --
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 said:
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 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.
 
K

Keith Thompson

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";''.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top