473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Is this assignment ok?

char *pA[5];
char *pB[5];

pA = pB;

This doesnt compile.

On second thoughts, does
(x) char *pA[5] mean
(a) "A pointer to 5 characters" or
(b) "5 pointers to a character" ?

If (x) == (b) then how would (a) be declared?

Any help would be appreciated.
Nov 13 '05 #1
16 2100
Colin JN Breame wrote:
char *pA[5];
char *pB[5];

pA = pB;

This doesnt compile.
Not surprisingly.

pA is the name of an array (an array of 5 pointers to char)
pB is the name of an array (an array of 5 pointers to char)

Neither pA nor pB is alterable, although their /contents/ are.
On second thoughts, does
(x) char *pA[5] mean
(a) "A pointer to 5 characters" or
(b) "5 pointers to a character" ?
(b)
If (x) == (b) then how would (a) be declared?
char (*pA)[5];
Any help would be appreciated.

--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 13 '05 #2
In article <bh**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Colin JN Breame <c.**********@durham.ac.uk> scribbled the following:
(a) "A pointer to 5 characters" or

If (x) == (b) then how would (a) be declared?


char (*pB)[5];


That's a pointer to (and array of) five pointers to characters.

A pointer to (an array of) 5 characters would be:
char *p[5];

-- Brett
Nov 13 '05 #3
>> On second thoughts, does
(x) char *pA[5] mean
is _roughly_ equivalent to
*pa0 *pa1 *pa2 *pa3 *pa4
(see argv hint hint!)
(a) "A pointer to 5 characters" or
(b) "5 pointers to a character" ?


It means 5 pointers to a character.


For me this is 5 pointers to a string (´aka "character array")
If (x) == (b) then how would (a) be declared?


char (*pB)[5];


Because IMO * takes precende before [] (does it? please help if I am incorrect)
I say it's 5 ptrs to "char*" and thus (*pb)[5] is equivalent to *pb[5]

--
- Jan Engelhardt
Nov 13 '05 #4
Brett Frankenberger <rb*@panix.com> scribbled the following:
In article <bh**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Colin JN Breame <c.**********@durham.ac.uk> scribbled the following:
(a) "A pointer to 5 characters" or
If (x) == (b) then how would (a) be declared?


char (*pB)[5];

That's a pointer to (and array of) five pointers to characters.
No, that's a pointer to an array of 5 characters.
A pointer to (an array of) 5 characters would be:
char *p[5];


No, that would make p an array of 5 pointers to a character.
For example:
char *p[5] = {"a", "b", "c", "d", "e"};

At least I *think* this is true. Feel free to correct me if you *know*
otherwise.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The truth is out there, man! Way out there!"
- Professor Ashfield
Nov 13 '05 #5
Jan Engelhardt <je*****@linux01.gwdg.de> scribbled the following:
If (x) == (b) then how would (a) be declared?
char (*pB)[5];

Because IMO * takes precende before [] (does it? please help if I am incorrect)
I say it's 5 ptrs to "char*" and thus (*pb)[5] is equivalent to *pb[5]


It's a pointer to an array of 5 chars. You are right that * takes
precedence over [], but parantheses ( and ) are a way of side-stepping
precedence. (*pb)[5] is not at all equivalent to *pb[5], any more than
1+2*3 is equivalent to (1+2)*3. (The first has value 7, the second has
value 9.)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Insanity is to be shared."
- Tailgunner
Nov 13 '05 #6
>>>> (a) "A pointer to 5 characters" or
If (x) == (b) then how would (a) be declared?
char (*pB)[5]; That's a pointer to (and array of) five pointers to characters.

No, that's a pointer to an array of 5 characters.
A pointer to (an array of) 5 characters would be:
char *p[5];


No, that would make p an array of 5 pointers to a character.
For example:
char *p[5] = {"a", "b", "c", "d", "e"};


Heh that's ... like reviceversa blah.
You say it's 5 pointers to *a* character, though you supply constant strings
(heh type "char *").
At least I *think* this is true. Feel free to correct me if you *know*
otherwise.


So as you can see yourself, you just declared "an array which holds 5 char*
things". To clearify:
typeof(p[0]) is "char *",
so your statment "5 pointers to a characters" cannot really hold true.

--
- Jan Engelhardt
Nov 13 '05 #7
Jan Engelhardt <je*****@linux01.gwdg.de> scribbled the following:
> (a) "A pointer to 5 characters" or
> If (x) == (b) then how would (a) be declared?
char (*pB)[5];
That's a pointer to (and array of) five pointers to characters.No, that's a pointer to an array of 5 characters.
A pointer to (an array of) 5 characters would be:
char *p[5];


No, that would make p an array of 5 pointers to a character.
For example:
char *p[5] = {"a", "b", "c", "d", "e"}; Heh that's ... like reviceversa blah.
You say it's 5 pointers to *a* character, though you supply constant strings
(heh type "char *").
The pointers point to the first characters of the constant strings,
in other words, to the characters 'a', 'b', 'c', 'd' and 'e'
respectively. That's how C strings work.
At least I *think* this is true. Feel free to correct me if you *know*
otherwise.

So as you can see yourself, you just declared "an array which holds 5 char*
things". To clearify:
typeof(p[0]) is "char *",
so your statment "5 pointers to a characters" cannot really hold true.


You appear to contradict yourself. If typeof(p[0]) is "char *" (which
is true), then p[0] is a pointer to a character, right? What would this
make p then, if not an array of pointers to a character?

Let me reiterate.
char *p[5];
p[0] is a pointer to a character.
p is an array of pointers to a character. (Or an array of pointers to
characters, if you want.)

On the other hand:
char (*p)[5];
*p is an array of characters. (5 characters, to be precise.)
p is a pointer to an array of characters (an array of 5 characters).

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"To know me IS to love me."
- JIPsoft
Nov 13 '05 #8


Jan Engelhardt wrote:

Because IMO * takes precende before [] (does it? please help if I am incorrect)
I say it's 5 ptrs to "char*" and thus (*pb)[5] is equivalent to *pb[5]


No. The [] operator has precedence over the * operator. Thus, they are
not equivalent.

I find it handy to have easy access to a Table of Precedence and
Associativity of Operators instead of depending on memory.

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #9
Colin JN Breame wrote:
char *pA[5];
char *pB[5];

pA = pB;

This doesnt compile.

On second thoughts, does
(x) char *pA[5] mean
(a) "A pointer to 5 characters" or
(b) "5 pointers to a character" ?

If (x) == (b) then how would (a) be declared?

Any help would be appreciated.


By the way, a pointer to 5 characters is the
same as a pointer to a single character.
char letters[5] = {'a', 'b', 'c', 'd', 'e'};
char * p;
p = letters; /* p now points to 5 letters. */

The variable 'p' above actually points to the
first of 5 letters. A pointer to 5 characters
will also point to the first of 5 characters.

Unless your being type specific, I would lose
the "pointer to 5 characters" and just use
a pointer to a character.

Another reason not to use a "pointer to 5
characters" is that the C language has no
automatic boundary checking on arrays. There is
nothing preventing you from pointing to the
fifth character then incrementing the pointer.
Dereferencing the pointer will lead to undefined
behavior, but still, there is no run-time check
to see that the pointer has been incremented past
the end of the array.

--
Thomas Matthews
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

Nov 13 '05 #10
In article <bh************@ID-169908.news.uni-berlin.de>, Al
Bowers wrote:
Jan Engelhardt wrote:
Because IMO * takes precende before [] (does it? please help
if I am incorrect) I say it's 5 ptrs to "char*" and thus
(*pb)[5] is equivalent to *pb[5]


No. The [] operator has precedence over the * operator. Thus,
they are not equivalent.

I find it handy to have easy access to a Table of Precedence
and Associativity of Operators instead of depending on memory.


Except in declarations they are not operators, they're
declarators. ;-)

--
Neil Cerutti
Nov 13 '05 #11
bd
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Colin JN Breame wrote:
char *pA[5];
char *pB[5];

pA = pB;

This doesnt compile.

On second thoughts, does
(x) char *pA[5] mean
(a) "A pointer to 5 characters" or
No. That'd be:
char *pA;
Then you'd point it to the first of those 5 characters, like so:
char array[5];
char *pA = array;
pA[3] = 42;

Or, to use dynamically allocated memory:
char *pA;
pA = malloc(sizeof *pa * 5);
if(!pA){
fprintf(stderr, "Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
pA[3] = 42;
/* ... */
free(pA);
pA = NULL; /* Often helps catch use of free()d memory */

This works because, in C, a[b] is the same as *(a + b), and addition of a
pointer and an integer works in multiples of the size of the pointed-to
type.
(b) "5 pointers to a character" ?

Correct.

It automatically decays to a pointer to its first element, but that's not a
lvalue, so you can't assign to it. Try this:
memcpy(pA, pB, sizeof pA);
- --
Freenet distribution not available
"Yes, it's the right planet, all right, " he said again.
"Right planet, wrong universe. "

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/Qousx533NjVSos4RAlY2AJ9BxTiSTrXQotMvqCd0vXeLRZMrEQ Cgu3bG
qvXZk1Cx6l1rb29MvbqP00A=
=HRO9
-----END PGP SIGNATURE-----
Nov 13 '05 #12

Colin JN Breame <c.**********@durham.ac.uk> wrote in message
news:pa****************************@dur.ac.uk...
char *pA[5];
char *pB[5];

pA = pB;

This doesnt compile.
Of course not. You cannot assign to an array.
Each element must be assigned individually.

for(size_t i = 0; i < sizeof pA / sizeof *pA; ++i)
pA[i] = pB[i];

On second thoughts, does
(x) char *pA[5] mean
(a) "A pointer to 5 characters" or
(b) "5 pointers to a character" ?
Array of five objects of type 'pointer to char'.

If (x) == (b) then how would (a) be declared?
Pointer to array of five characters:

char (*arr)[5];

An array of five characters:

char arr[5];

Pointer to any character, be it an array element
or not:

char *p;

Assign to 'p' the address of the first element of
array:

p = arr;


Any help would be appreciated.


Which C book(s) are you studying?

-Mike

Nov 13 '05 #13

Brett Frankenberger <rb*@panix.com> wrote in message
news:bh**********@reader1.panix.com...
In article <bh**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Colin JN Breame <c.**********@durham.ac.uk> scribbled the following:
(a) "A pointer to 5 characters" or

If (x) == (b) then how would (a) be declared?


char (*pB)[5];


That's a pointer to (and array of) five pointers to characters.

A pointer to (an array of) 5 characters would be:
char *p[5];


BZZT!!

Try again.

-Mike
Nov 13 '05 #14

"bd" <bd*****@users.sf.net> wrote in message
news:em************@bd-home-comp.no-ip.org...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Colin JN Breame wrote:
char *pA[5];
char *pB[5];

pA = pB;

This doesnt compile.

On second thoughts, does
(x) char *pA[5] mean
(a) "A pointer to 5 characters" or
No. That'd be:
char *pA;
Then you'd point it to the first of those 5 characters, like so:
char array[5];
char *pA = array;
pA[3] = 42;

Or, to use dynamically allocated memory:
char *pA;
pA = malloc(sizeof *pa * 5);


I think the 'sizeof' is not needed here. The size of char is defined as 1
byte.

pA = malloc(5);

if(!pA){
fprintf(stderr, "Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
pA[3] = 42;
/* ... */
free(pA);
pA = NULL; /* Often helps catch use of free()d memory */

This works because, in C, a[b] is the same as *(a + b), and addition of a
pointer and an integer works in multiples of the size of the pointed-to
type.
(b) "5 pointers to a character" ? Correct.

It automatically decays to a pointer to its first element, but that's not

a lvalue, so you can't assign to it. Try this:
memcpy(pA, pB, sizeof pA);
- --
Freenet distribution not available
"Yes, it's the right planet, all right, " he said again.
"Right planet, wrong universe. "

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/Qousx533NjVSos4RAlY2AJ9BxTiSTrXQotMvqCd0vXeLRZMrEQ Cgu3bG
qvXZk1Cx6l1rb29MvbqP00A=
=HRO9
-----END PGP SIGNATURE-----


--
Jeff
Nov 13 '05 #15
>
Which C book(s) are you studying?

I have a copy of K&R but dont often find it very useful.
Nov 13 '05 #16
Colin JN Breame wrote:

Which C book(s) are you studying?

I have a copy of K&R but dont often find it very useful.


Most of the experienced programmers here find that to be very useful.
That should tell you something.


Brian Rodenborn
Nov 13 '05 #17

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

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
10
by: Andrew Koenig | last post by:
It has been pointed out to me that various C++ books disagree about the relative precedence of ?: and the assignment operators. In order to satisfy myself about the matter once and for all, I...
5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
1
by: Jon Slaughter | last post by:
I have a chain of classes(i.e., a series of classes each containing an array of the next class). Each class has array like access. struct Myclass1 { vector(Myclass2) _Myclass2; Myclass2&...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
35
by: nagy | last post by:
I do the following. First create lists x,y,z. Then add an element to x using the augumented assignment operator. This causes all the other lists to be changed also. But if I use the assignment...
20
by: TimeHorse | last post by:
I would like to gauge interest in the following proposal: Problem: Assignment statements cannot be used as expressions. Performing a list of mutually exclusive checks that require data...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.