J
Jasper Dozer
Is this a healthy way to get a pointer to point ?
char *p = "longenough";
regards,
jasper
char *p = "longenough";
regards,
jasper
Jasper Dozer said:Is this a healthy way to get a pointer to point ?
char *p = "longenough";
regards,
jasper
Jasper said:Is this a healthy way to get a pointer to point ?
char *p = "longenough";
regards,
jasper
Jasper said:Is this a healthy way to get a pointer to point ?
char *p = "longenough";
Depends on what you want to do. Do you know what string literals are,
and how they may be used and may not be used? The name of the literal
you've chosen bothers me, it seems like you think this is a quick way to
dynamically allocate space for some other use. DO NOT DO THAT!
In most of the cases, memory for the string (where p points) is
allocated in a ro memory area, which will result in an error if you
try to change the contents of that area where p points to.
Cheers,
AG
Thanks for your replies. No I do not know what a sting literal is and
how the are to be used. Neither do i have an idea what a ro memory
area is. I am sorry that my initial question was so unclear. Let me be
more specific. The reason that I asked this is because I found the
following piece of code in software is used daily in our lab:
static char *statename= "NOTINITIALIZEDYETBUTLONGENOUGH";
later on the program statename is used in an video interrupt loop like
this
...
statename="WAITBLOCKREADY";
...
statename="SHOWSTIMULUS"
...
etc.
which is used outside the videointerrupt to
Str255 str; /* typedef unsigned char Str255[256]; */
sprintf( (char *)str, "%s", staten );
the &str is used to plot the statename to a dialog window with a
function that takes a pointer to a Str55 as an argument.
So Brian Rodenborn was right when he thought this was used as a trick
to easily allocate memory for other use. So what should I do instead?
Will this be ok?
char *statename;
in an initialisation function that can be called more than once:
void initfunc()
{
if (statename!=NULL) {
free(statename);
}
statename = malloc(100);
if (statename==NULL) {
// give an error and quit the program
}
}
in the videointerrupt:
if (statename!=NULL) {
strcpy( statename, "WAITBLOCKREADY");
}
and use &statename to plot the statename in the dialog. (outside the
videointerrupt)
and finally
free(statename);
Thanks for your replies. No I do not know what a sting literal is and
how the are to be used. Neither do i have an idea what a ro memory
area is. I am sorry that my initial question was so unclear. Let me be
more specific. The reason that I asked this is because I found the
following piece of code in software is used daily in our lab:
static char *statename= "NOTINITIALIZEDYETBUTLONGENOUGH";
later on the program statename is used in an video interrupt loop like
this
...
statename="WAITBLOCKREADY";
...
statename="SHOWSTIMULUS"
...
etc.
which is used outside the videointerrupt to
Str255 str; /* typedef unsigned char Str255[256]; */
sprintf( (char *)str, "%s", staten );
the &str is used to plot the statename to a dialog window with a
function that takes a pointer to a Str55 as an argument.
So Brian Rodenborn was right when he thought this was used as a trick
to easily allocate memory for other use. So what should I do instead?
Will this be ok?
char *statename;
in an initialisation function that can be called more than once:
void initfunc()
{
if (statename!=NULL) {
free(statename);
}
statename = malloc(100);
if (statename==NULL) {
// give an error and quit the program
}
}
in the videointerrupt:
if (statename!=NULL) {
strcpy( statename, "WAITBLOCKREADY");
}
and use &statename to plot the statename in the dialog. (outside the
videointerrupt)
Thanks for your replies. No I do not know what a sting literal is and
how the are to be used. Neither do i have an idea what a ro memory
area is. I am sorry that my initial question was so unclear. Let me be
more specific. The reason that I asked this is because I found the
following piece of code in software is used daily in our lab:
static char *statename= "NOTINITIALIZEDYETBUTLONGENOUGH";
later on the program statename is used in an video interrupt loop like
this
...
statename="WAITBLOCKREADY";
...
statename="SHOWSTIMULUS"
...
etc.
which is used outside the videointerrupt to
Str255 str; /* typedef unsigned char Str255[256]; */
sprintf( (char *)str, "%s", staten );
the &str is used to plot the statename to a dialog window with a
function that takes a pointer to a Str55 as an argument.
So Brian Rodenborn was right when he thought this was used as a trick
to easily allocate memory for other use. So what should I do instead?
Thanks for your replies. No I do not know what a sting literal is and
how the are to be used. Neither do i have an idea what a ro memory
area is.
Let me be
more specific. The reason that I asked this is because I found the
following piece of code in software is used daily in our lab:
static char *statename= "NOTINITIALIZEDYETBUTLONGENOUGH";
later on the program statename is used in an video interrupt loop like
this
...
statename="WAITBLOCKREADY";
...
statename="SHOWSTIMULUS"
...
etc.
which is used outside the videointerrupt to
Str255 str; /* typedef unsigned char Str255[256]; */
sprintf( (char *)str, "%s", staten );
So Brian Rodenborn was right when he thought this was used as a trick
to easily allocate memory for other use. So what should I do instead?
Will this be ok?
char *statename;
in an initialisation function that can be called more than once:
void initfunc()
{
if (statename!=NULL) {
free(statename);
}
statename = malloc(100);
if (statename==NULL) {
// give an error and quit the program
}
}
in the videointerrupt:
if (statename!=NULL) {
strcpy( statename, "WAITBLOCKREADY");
}
and use &statename to plot the statename in the dialog. (outside the
videointerrupt)
and finally
free(statename);
static char *statename= "NOTINITIALIZEDYETBUTLONGENOUGH";
later on the program statename is used in an video interrupt loop like
this
statename="WAITBLOCKREADY";
Joona I Palaste said:Thanks for your replies. No I do not know what a sting literal is and
how the are to be used. Neither do i have an idea what a ro memory
area is. I am sorry that my initial question was so unclear. Let me be
more specific. The reason that I asked this is because I found the
following piece of code in software is used daily in our lab:
A string literal is a piece of C code somewhat like this:
""
"Hello, world!\n"
"longenough"
"You have performed an illegal operation. We have informed Microsoft\
General HQ of the recent activities. Resistance is futile."
String literals are objects of type char[], with the length of the
array being the length of the string plus one char for the terminating
'\0'.
static char *statename= "NOTINITIALIZEDYETBUTLONGENOUGH";later on the program statename is used in an video interrupt loop like
this...
statename="WAITBLOCKREADY";
...
statename="SHOWSTIMULUS"
...
etc.
Video interrupts are off-topic for comp.lang.c. But luckily your
question does not depend on knowledge about video interrupts, but can
be solved entirely within the domain of standard C.
which is used outside the videointerrupt to
Str255 str; /* typedef unsigned char Str255[256]; */
Don't do that. (Or if you didn't do that, tell the one who did not to
do that.) Such typedefs are confusing. Why don't you simply type:
unsigned char str[256];
or:
#define STRSIZE 256
unsigned char str[STRSIZE];
?
You don't have to cast str. It will automatically "decay" into a char *
when used as a parameter.
Surely you mean statename and not staten?
What &str? I don't see any &str in the above code. And also, don't
you mean Str255 and not Str55?
Lastly, plotting and dialog windows are off-topic on comp.lang.c.
Jasper Dozer said:(snip)
Str255 str; /* typedef unsigned char Str255[256]; */
Don't do that. (Or if you didn't do that, tell the one who did not to
do that.) Such typedefs are confusing. Why don't you simply type:
unsigned char str[256];
or:
#define STRSIZE 256
unsigned char str[STRSIZE];
?
Str255 is a typedef that engineers at apple think is usefull. I don't
know these guys so I will not be able to them their idea sucks. In the
future I will not comment on system specific typedefs but replace them
by ANSI C before posting to comp.lang.c
---REASON FOR POSTING THIS REPLY:
when I remove the cast my compiler issues the following error:
illegal implicit conversion from 'unsigned char[256] to 'char *'
Is this specific for my compiler or would something similar happen in
pure ANSI C? In other words, are you sure it will automatically
"decay" into a char* when used as a parameter?
j said:Joona I Palaste said:Jasper Dozer said:Joona I Palaste <[email protected]> wrote in messageJasper Dozer <[email protected]> scribbled the following:
Str255 str; /* typedef unsigned char Str255[256]; */
Don't do that. (Or if you didn't do that, tell the one who did not to
do that.) Such typedefs are confusing. Why don't you simply type:
unsigned char str[256];
or:
#define STRSIZE 256
unsigned char str[STRSIZE];
?Str255 is a typedef that engineers at apple think is usefull. I don't
know these guys so I will not be able to them their idea sucks. In the
future I will not comment on system specific typedefs but replace them
by ANSI C before posting to comp.lang.c
It is not a system specific typedef, its behaviour is fully defined
within ANSI C. So it's not *wrong* to post it to comp.lang.c. Only I
personally find it ugly.
I believe under c90 that (str[A-Z][0-9]{3}) is reserved for the
implementation.
Also, if I remember correctly, the first three characters for ``str'' is
case insensitive
So for c89/90 ([sS][tT][rR][A-Z][0-9]{3}) is reserved for the
implementation.
Joona I Palaste said:Jasper Dozer said:Joona I Palaste <[email protected]> wrote in messageJasper Dozer <[email protected]> scribbled the following:
(e-mail address removed) (Amarendra GODBOLE) wrote in message(snip)Is this a healthy way to get a pointer to point ?
char *p = "longenough";
Depends on what you want to do. Do you know what string literals are,
and how they may be used and may not be used? The name of the literal
you've chosen bothers me, it seems like you think this is a quick way to
dynamically allocate space for some other use. DO NOT DO THAT!
In most of the cases, memory for the string (where p points) is
allocated in a ro memory area, which will result in an error if you
try to change the contents of that area where p points to.
Str255 str; /* typedef unsigned char Str255[256]; */
Don't do that. (Or if you didn't do that, tell the one who did not to
do that.) Such typedefs are confusing. Why don't you simply type:
unsigned char str[256];
or:
#define STRSIZE 256
unsigned char str[STRSIZE];
?Str255 is a typedef that engineers at apple think is usefull. I don't
know these guys so I will not be able to them their idea sucks. In the
future I will not comment on system specific typedefs but replace them
by ANSI C before posting to comp.lang.c
It is not a system specific typedef, its behaviour is fully defined
within ANSI C. So it's not *wrong* to post it to comp.lang.c. Only I
personally find it ugly.
---REASON FOR POSTING THIS REPLY:
when I remove the cast my compiler issues the following error:illegal implicit conversion from 'unsigned char[256] to 'char *'
Is this specific for my compiler or would something similar happen in
pure ANSI C? In other words, are you sure it will automatically
"decay" into a char* when used as a parameter?
Actually my answer was only partially correct. It will, in fact,
decay into a "unsigned char *". Some compilers permit automatic
conversion from unsigned char * to char *, but some don't.
*Any* array of type T will decay into a pointer to type T, where T
is any type. This applies both to ANSI C and pre-ANSI C. However the
point I initially missed was that char and unsigned char are not
*necessarily* the same type.
(snip)
--
/-- 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! ------------/
"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
- Bob Wiley
Joona I Palaste said:j said:Joona I Palaste said:Jasper Dozer <[email protected]> scribbled the following:
Joona I Palaste <[email protected]> wrote in messageJasper Dozer <[email protected]> scribbled the following:
Str255 str; /* typedef unsigned char Str255[256]; */
Don't do that. (Or if you didn't do that, tell the one who did not to
do that.) Such typedefs are confusing. Why don't you simply type:
unsigned char str[256];
or:
#define STRSIZE 256
unsigned char str[STRSIZE];
?
Str255 is a typedef that engineers at apple think is usefull. I don't
know these guys so I will not be able to them their idea sucks. In the
future I will not comment on system specific typedefs but replace them
by ANSI C before posting to comp.lang.c
It is not a system specific typedef, its behaviour is fully defined
within ANSI C. So it's not *wrong* to post it to comp.lang.c. Only I
personally find it ugly.I believe under c90 that (str[A-Z][0-9]{3}) is reserved for the
implementation.
Also, if I remember correctly, the first three characters for ``str'' is
case insensitive
So for c89/90 ([sS][tT][rR][A-Z][0-9]{3}) is reserved for the
implementation.
We're lucky "Str255" fails to meet this regular expression then, as
"Str" does match [sS][tT][rR] but "2" isn't any letter from A to Z,
or any letter from a to z, that I've ever heard of.
[snip]
Str255 str; /* typedef unsigned char Str255[256]; */
Don't do that. (Or if you didn't do that, tell the one who did not to
do that.) Such typedefs are confusing. Why don't you simply type:
unsigned char str[256];
or:
#define STRSIZE 256
unsigned char str[STRSIZE];
?
Str255 is a typedef that engineers at apple think is usefull. I don't
know these guys so I will not be able to them their idea sucks. In the
future I will not comment on system specific typedefs but replace them
by ANSI C before posting to comp.lang.c
IIRC, Str255 was meant to be used as a Pascal-style string (leading
length byte) instead of a C-style string (zero-terminated array). I
haven't done a ton of Mac programming, but from what I remember the
API calls expected Pascal-style strings, and there were several
routines to convert from C to Pascal strings, and Str255 was used for
the Pascal strings.
C strings should simply be typed as char[].
[ typedef Str255 for unsigned char [256] ]news:[email protected]...
Any valid identifier beginning str[a-z], thus str[a-z][a-zA-Z0-9_]*,I believe under c90 that (str[A-Z][0-9]{3}) is reserved for the
implementation.
Also, if I remember correctly, the first three characters for ``str'' is
case insensitive
So for c89/90 ([sS][tT][rR][A-Z][0-9]{3}) is reserved for the
implementation.
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.