pointer to string

J

junky_fellow

guys,

I need to declare a pointer to a string. Which of the following is
better and why?

char *pStr;
unsigned char *pStr;

thanks for any help ...
 
N

Nick Keighley

 I need to declare a pointer to a string. Which of the following is
better and why?

char *pStr;
unsigned char *pStr;

in C a string is zero terminated array of char. So char* seems
sensible. Since string literals are unmodifiable consider
using const char*. If you are dealing with "bytes" (or octets)
from a comms link or socket consider unsigned char*.

char my_str [] = "hello";
char *my_other_str = "there";
char *str_ptr = my_str;
unsigned char *packet;

packet = read_from_sock (sock_handle);
 
H

Herbert Rosenau

guys,

I need to declare a pointer to a string. Which of the following is
better and why?

Depends what you really needs. Does it not matter if the chars in the
string are signed or not?
char *pStr;

You would like to define that any char is definitely unsigned and you
don't trust that your environment makes char definitely unsigned,
except you tells it otherwise?
unsigned char *pStr;

You have to be sure that any char in the string is signed?

signed char *pStr;

However I use a compiler option to have any string unsigned as default
- except I use 'signed' explicity. You will have to ask the
documentation of your compiler for the flag to define 'char' as 'be
always unsigned'. Typical usase of a char is unsigned for printout
(display on screen). Typical use for using char as short short
mathematical data type is 'signed char'. As the standard lets it
completely to the implementation to use signed or unsigned char when
'char' is used the behavior for 'char' is either 'signed char' or
'unsigned char' depending on the documentation of your compiler.

On other hand you compiler will have a flag (e.g. #pragma or a
commandline option) to set a specific behavior of (pointer to) 'char'.
At lest it is on you, the developer, to be sure to have a clearly
specified behavior of (un)singned behavior for the whole program and
use explicity 'signed' respective 'unsigned char' for cases you must
use the contrary to the (selected) default.

That


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
S

sid

guys,

 I need to declare a pointer to a string. Which of the following is
better and why?

char *pStr;
unsigned char *pStr;

thanks for any help ...

I am not quite sure but I would like to specify that the unsigned is
compiler dependent and if you think that you can use all of the bits,
the compiler may implicitly convert it into signed. So always be
careful when you use an unsigned data type.
 
C

Chris Dollin

sid said:
I am not quite sure but I would like to specify that the unsigned is
compiler dependent and if you think that you can use all of the bits,
the compiler may implicitly convert it into signed. So always be
careful when you use an unsigned data type.

What?

I don't understand what you're trying to say -- can you give a more
specific example?
 
K

kooladi

guys,

I need to declare a pointer to a string. Which of the following is
better and why?

char *pStr;
unsigned char *pStr;

thanks for any help ...

Both "should" work...however...
The concept of signs makes no sense to me when applied to a String. If
you want to declare a pointer to a string, go with char* .
If,however,you want to manipulate a decimal value and you think char(a
signed char) is too small and Int is too big, then go for unsigned
char.

Besides if you do something like unsigned char* pStr = "Hi There",
some compliers might throw an assignment type mismatch warning.


Adi
 
C

Chris Dollin

kooladi said:
Both "should" work...however...
The concept of signs makes no sense to me when applied to a String.

That's OK, because its not the string that's signed; it's the
component characters.
If
you want to declare a pointer to a string, go with char* .

Because that's what the C string operations expect.
If,however,you want to manipulate a decimal value and you think char(a
signed char) is too small and Int is too big, then go for unsigned
char.

What's "decimal" got to do with it? How would you represent -1 as
an unsigned char?

Don't forget the flavours of `short`.
Besides if you do something like unsigned char* pStr = "Hi There",
some compliers might throw an assignment type mismatch warning.

Shouldn't they /all/?

--
"It was the first really clever thing the King had /Alice in Wonderland/
said that day."

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
K

Keith Thompson

Nick Keighley said:
in C a string is zero terminated array of char. So char* seems
sensible. Since string literals are unmodifiable consider
using const char*. If you are dealing with "bytes" (or octets)
from a comms link or socket consider unsigned char*.
[...]

If you're dealing with sequences of octets, they're probably not
terminated by a '\0', and therefore they don't constitute a string.

Of course you *might* happen to have an externally generated data
format consisting of a sequence of unsigned bytes terminated by a zero
byte, but such things aren't very common as far as I know; the size of
such a format is usually determined in some other way, particularly if
it needs to be able to hold arbitrary data. (And of course plain char
might be unsigned, but that's beside the point.)
 
K

Keith Thompson

Chris Dollin said:
kooladi wrote: [...]
If,however,you want to manipulate a decimal value and you think char(a
signed char) is too small and Int is too big, then go for unsigned
char.

What's "decimal" got to do with it? How would you represent -1 as
an unsigned char?

<correct_but_not_helpful> UCHAR_MAX </correct_but_not_helpful>
 

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,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top