Is this assignment ok?

L

Lew Pitcher

Colin said:
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)
 
J

Jan Engelhardt

On second thoughts, does
(x) char *pA[5] mean

is _roughly_ equivalent to
*pa0 *pa1 *pa2 *pa3 *pa4
(see argv hint hint!)
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]
 
C

Colin JN Breame

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.
 
J

Joona I Palaste

Brett Frankenberger said:
Colin JN Breame said:
(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 ([email protected]) ---------------------------\
| 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
 
J

Joona I Palaste

Jan Engelhardt said:
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 ([email protected]) ---------------------------\
| 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
 
J

Jan Engelhardt

(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.
 
J

Joona I Palaste

Jan Engelhardt said:
(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.
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 ([email protected]) ---------------------------\
| 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
 
A

Al Bowers

Jan said:
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.
 
T

Thomas Matthews

Colin said:
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.
 
N

Neil Cerutti

Jan said:
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. ;-)
 
B

bd

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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 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/Qousx533NjVSos4RAlY2AJ9BxTiSTrXQotMvqCd0vXeLRZMrEQCgu3bG
qvXZk1Cx6l1rb29MvbqP00A=
=HRO9
-----END PGP SIGNATURE-----
 
M

Mike Wahler

Colin JN Breame said:
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 = pB;
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
 
J

Jeff

bd said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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 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/Qousx533NjVSos4RAlY2AJ9BxTiSTrXQotMvqCd0vXeLRZMrEQCgu3bG
qvXZk1Cx6l1rb29MvbqP00A=
=HRO9
-----END PGP SIGNATURE-----
 
D

Default User

Colin said:
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
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top