472,371 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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
Jun 27 '08 #1
10 3276
Ahmad Humayun <ah*********@gmail.comwrote:
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
Jun 27 '08 #2
Ahmad Humayun <ah*********@gmail.comwrites:
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"
Jun 27 '08 #3
On May 26, 1:04*am, Keith Thompson <ks...@mib.orgwrote:
Ahmad Humayun <ahmad.hu...@gmail.comwrites:
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 helpful :)

Happy coding :)
Jun 27 '08 #4
On May 26, 1:04*am, Keith Thompson <ks...@mib.orgwrote:
Ahmad Humayun <ahmad.hu...@gmail.comwrites:
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 helpful :)

Happy coding :)
Jun 27 '08 #5
On May 25, 10:37 pm, Ahmad Humayun <ahmad.hu...@gmail.comwrote:
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;
}
Jun 27 '08 #6
4On Tue, 27 May 2008 06:20:12 -0700 (PDT), vi******@gmail.com wrote:
>On May 25, 10:37 pm, Ahmad Humayun <ahmad.hu...@gmail.comwrote:
>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
Jun 27 '08 #7
On May 28, 4:58 am, Barry Schwarz <schwa...@dqel.comwrote:
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.comwrote:
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/>
Jun 27 '08 #8
vi******@gmail.com writes:
On May 28, 4:58 am, Barry Schwarz <schwa...@dqel.comwrote:
>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.comwrote:
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) 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"
Jun 27 '08 #9
On May 28, 8:11 pm, Keith Thompson <ks...@mib.orgwrote:
vipps...@gmail.com writes:
On May 28, 4:58 am, Barry Schwarz <schwa...@dqel.comwrote:
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.comwrote:
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.comwrote:
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.comwrote:
>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.
Jun 27 '08 #10
vi******@gmail.com writes:
On May 28, 8:11 pm, Keith Thompson <ks...@mib.orgwrote:
[...]
>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) 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"
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Arthur Sinko | last post by:
Hi, Is it possible to answer the following question: what is the difference between two declarations: vector<double> a(n); and double a;
7
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
24
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
9
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
11
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
2
by: Ma Xiaoming | last post by:
Dear ladies and gentlemen, I don't understand what the difference between the Heap and the Stack is. Could you please explain the difference between the both for me? Thank you very much. ...
12
by: Veeru | last post by:
Hi, Can anyone tell me the difference between heap memory and stack stack both physically and fundamentally. It would be a real favour. Regards, Veeru
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.