Hello all:
What's the difference between p and q in the following statements?
char p[] = "Hello";
char *q = "Hello";
I know q stores the address of 'H'.
Question is: does p store the address of 'H' too?
I know p is the name of the array that contains "Hello". Is array name
a pointer?
In other words, is p exactly the same as &p[0]?
p and q are the same if you want to print them out by %s.
Is there a case where p and q (array name and pointer to the array)
can not be used interchangably? I knew there is, such as sizeof( ).
p.s. I remembered reading somewhere on the Net says that the statement
char *q = "Hello"
is not a good style of programming. coz you do not know whether q
points to a valid address or not.
Is it true? 7 3445 ia***********@g mail.com wrote:
Hello all:
What's the difference between p and q in the following statements?
char p[] = "Hello";
char *q = "Hello";
I know q stores the address of 'H'.
Question is: does p store the address of 'H' too?
I know p is the name of the array that contains "Hello". Is array name
a pointer?
In other words, is p exactly the same as &p[0]?
p and q are the same if you want to print them out by %s.
Is there a case where p and q (array name and pointer to the array)
can not be used interchangably? I knew there is, such as sizeof( ).
One difference is that *q = ? [or q[0] = ???] may not work - as "Hello" here
may *not* be writeable [readonly] - however *p = ? and p[0] = ? will work -
it's shorthand for char p[] = { 'H', 'e', ... };, i.e., simple array
initialisation. Anyway, yes, p stores the address of *its* 'H' as does q.
p.s. I remembered reading somewhere on the Net says that the statement
char *q = "Hello"
is not a good style of programming. coz you do not know whether q
points to a valid address or not.
Is it true?
No - if the code compiles, and there isn't some catastrophic runtime error,
then q points to the address where 'H' is stored.
<ia***********@ gmail.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hello all:
What's the difference between p and q in the following statements?
char p[] = "Hello";
Here's a paraphrase of what this means: "Mr. Compiler Sir: Please create an
array of 6 char's, which I will refer to as 'p'. Also, make its initial
contents the chars 'H', 'e', 'l', 'l', 'o', 0. Oh, and I might just write
new values in the array, if I feel like it"
char *q = "Hello";
Here's a paraphrase of what this means: "Mr. Compiler Sir: Please create a
pointer to char, which I will refer to as 'q'. Also, make its initial
contents point to the first char of an unnamed array of 6 chars with the
initial contents 'H', 'e', 'l', 'l', 'o', 0. I promise not to write into
the unnamed array, but I might just point q somewhere else".
>
I know q stores the address of 'H'.
Question is: does p store the address of 'H' too?
No; p is an array, not a pointer.
I know p is the name of the array that contains "Hello". Is array name
a pointer?
No, it is not. In many contexts, an array name is automatically converted
into a pointer to the first element, but not all.
In other words, is p exactly the same as &p[0]?
No, not exactly. For example:
sizeof(p) == 6
sizeof(&p[0]) == sizeof (char *)
>
p and q are the same if you want to print them out by %s.
Is there a case where p and q (array name and pointer to the array)
can not be used interchangably? I knew there is, such as sizeof( ).
Here's two others:
p = "Bye!"; /* Error! */
q = "Bye!"; /* Works great! */
And:
p[0] = 'J'; /* Yummm! */
q[0] = 'J'; /* Undefined behavior (unless you've set q to point to a
writable char) */
>
p.s. I remembered reading somewhere on the Net says that the statement
char *q = "Hello"
is not a good style of programming. coz you do not know whether q
points to a valid address or not.
Is it true?
Nonsense. Look at the paraphrase of the definition of q I gave above --
you told the compiler to point q at a valid memory location (the first char
of the unnamed array).
>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 ia***********@g mail.com wrote:
Hello all:
What's the difference between p and q in the following statements?
char p[] = "Hello";
char *q = "Hello";
I know q stores the address of 'H'.
Question is: does p store the address of 'H' too?
No. p stores an array 6 of characters.
I know p is the name of the array that contains "Hello". Is array name
a pointer?
No, but it can often be treated /as if/ it were a pointer.
In other words, is p exactly the same as &p[0]?
It depends on the context.
sizeof(p) will return 6 (the length of the reserved space), and
sizeof (&p[0]) will return the size of a character pointer
but
*p is equal to 'H', and
*(&p[0]) is also equal to 'H'
p and q are the same if you want to print them out by %s.
Is there a case where p and q (array name and pointer to the array)
can not be used interchangably? I knew there is, such as sizeof( ).
So you already know that p is /not/ a pointer.
p.s. I remembered reading somewhere on the Net says that the statement
char *q = "Hello"
is not a good style of programming. coz you do not know whether q
points to a valid address or not.
Is it true?
Not necessarily. You left out a lot of context.
Often
char *q = "Hello";
is better style than
char p[] = "Hello";
when you want the string to not be modifiable (initially).
But, because neither variable is declared as non-modifiable,
p can be overwritten with a string of 0 to 5 characters + the EOS,
and
q can be overwritten with a pointer to something (either NULL or
a real object)
HTH
- --
Lew Pitcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12
iD8DBQFFIUZ9agV FX4UWr64RAuMJAK DjlafFWoEsc2+Yy ZzrTjxYEKz4GACf ULBM
+TThgTgF4qKJVzH 9lGcNxoQ=
=Qd3q
-----END PGP SIGNATURE----- ia***********@g mail.com said:
Hello all:
What's the difference between p and q in the following statements?
char p[] = "Hello";
char *q = "Hello";
p is an aggregate object, six bytes in size. sizeof p will confirm this. The
bytes contain 'H', 'e', 'l', 'l', 'o', and the null terminator character
'\0', in that order.
q is a pointer, its type being "pointer to char". Its value refers to the
place in memory where the string literal "Hello" (a six-character array
with static storage duration) is located.
I know q stores the address of 'H'.
That's a rather informal way of putting it. :-) Specifically, it stores the
address of the particular 'H' that the compiler inserts somewhere in memory
and directly follows with 'e', 'l', 'l', 'o', and the null terminator
character '\0'.
Question is: does p store the address of 'H' too?
No.
I know p is the name of the array that contains "Hello". Is array name
a pointer?
No, but it is treated like one most of the time. Exceptions: sizeof p and &p
In other words, is p exactly the same as &p[0]?
In a value context, yes. Consider that p[i] and *(p + i) mean the same
thing, recognise that & and * are complementary (and therefore cancel), and
set i to 0:
p[i] means the same as *(p + i)
&p[i] means the same as &*(p + i)
&p[i] means the same as (p + i)
&p[0] means the same as (p + 0)
&p[0] means the same as (p )
&p[0] means the same as p
p and q are the same if you want to print them out by %s.
If you mean printf will produce the same output for either when you pass
them as a match for the %s format specifier, you're right.
Is there a case where p and q (array name and pointer to the array)
can not be used interchangably? I knew there is, such as sizeof( ).
It's sizeof, not sizeof(). Yes, the other case is &
p.s. I remembered reading somewhere on the Net says that the statement
char *q = "Hello"
is not a good style of programming.
Right. Better: const char *q = "Hello";
coz you do not know whether q points to a valid address or not.
Is it true?
No, the reason is that you mustn't modify the string literal. The const
helps the compiler to remind you not to do that.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Scott Fluhrer said:
>
<ia***********@ gmail.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
>Hello all:
What's the difference between p and q in the following statements?
char p[] = "Hello";
Here's a paraphrase of what this means: "Mr. Compiler Sir: Please create
an
array of 6 char's, which I will refer to as 'p'. Also, make its initial
contents the chars 'H', 'e', 'l', 'l', 'o', 0. Oh, and I might just write
new values in the array, if I feel like it"
SCOTT! Ltnc! Welcome back.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
"Richard Heathfield" <in*****@invali d.invalidwrote in message
news:Oe******** *************** *******@bt.com. ..
Scott Fluhrer said:
>> <ia*********** @gmail.comwrote in message news:11******* *************** @i3g2000cwc.goo glegroups.com.. .
>>Hello all:
What's the difference between p and q in the following statements?
char p[] = "Hello";
Here's a paraphrase of what this means: "Mr. Compiler Sir: Please create an array of 6 char's, which I will refer to as 'p'. Also, make its initial contents the chars 'H', 'e', 'l', 'l', 'o', 0. Oh, and I might just write new values in the array, if I feel like it"
SCOTT! Ltnc! Welcome back.
Thanks. I've been busy with other things, but I'll be lurking from time to
time.
--
poncho ia***********@g mail.com writes:
Hello all:
What's the difference between p and q in the following statements?
char p[] = "Hello";
char *q = "Hello";
Have you read section 6 of the comp.lang.c FAQ <http://www.c-faq.com/>?
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: maniac |
last post by:
Hey guys, I'm new here, just a simple question.
I'm learning to Program in C, and I was recommended a book called,
"Mastering C Pointers", just asking if any of you have read it,
and if it's worth the $25USD.
I'm just looking for a book on Pointers, because from what I've
read it's one of the toughest topics to understand.
thanks in advanced.
|
by: Me |
last post by:
Just a question/observation out of frustration.
I read in depth the book by Peter Van Der Linden entitled
"Expert C Programming" (Deep C Secrets). In particular the
chapters entitled:
4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
9: More about Arrays
10: More about Pointers
What blows me out of the water is the fact that 'every' programmer
|
by: Richard Hayden |
last post by:
Hi,
Why does gcc (3.3.2) give me a 'initialization from incompatible pointer
type' warning when compiling:
int main(int argc, char** argv) {
int testa;
int** testp = testa;
}
|
by: raphfrk |
last post by:
I have the following code:
char buf;
printf("%lp\n", buf);
printf("%lp\n", &buf);
printf("%lp\n", buf);
printf("%lp\n", buf);
printf("%d\n", buf-buf);
|
by: code break |
last post by:
what is the difference in this pointers decalarition ?
int *ptr;
and
int (*ptr);
| |
by: Martin Jørgensen |
last post by:
Hi,
I'm relatively new with C-programming and even though I've read about
pointers and arrays many times, it's a topic that is a little confusing
to me - at least at this moment:
----
1) What's the difference between these 3 statements:
(i) memcpy(&b, &KoefD, n); // this works somewhere in my code
|
by: sandy |
last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)
Each Directory can have subdirectories so I thought to put these in an
array. The application compiles but aborts without giving me any useful
information.
What I suspect is happening is infinite recursion. Each Directory
object creates an array of Subdirectories each of which has an array of...
|
by: Slain |
last post by:
I am a beginner and have some confusion with respect to pointers and
strings. It seems that the pointers with dealing with integer arrays
behave differently, as opposed to strings. Can some one explain me the
difference?
Sample Program:
int main()
{
int array={1,2,3,4,5};
|
by: DiAvOl |
last post by:
Hello everyone, merry christmas!
I have some questions about the following program:
arrtest.c
------------
#include <stdio.h>
int main(int agc, char *argv) {
|
by: David Thompson |
last post by:
On Wed, 09 Apr 2008 12:34:43 +0500, arnuld <arnVuld@ippiVmail.com>
wrote:
I think you've got the idea, but:
- I would be careful about using 'equal'. Pointers and arrays are
different things, but they only way in C to determine equality of two
things is an expression using the == operator, and _in an expression_
an array turns into a pointer which when compared to another pointer
can indeed be equal. Since your sig refers to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |