strncpy to a char member of struct

A

anars

Hi,

I have this struct initialized globally:
struct riddle {
char *text;
....
}

below, the function I call in main with the riddle struct as arg. is
defined:
void prepareRiddle(struct riddle *riddle)
{
// here I get a string (char *tmp) with a known size (int length)
incl. terminator.
riddle->text = (char*)malloc(length * sizeof(char)); // figured
sizeof(char) could be != 1 on some platforms
....
}

still, sizeof(riddle.text) called in main() doesn't give me the numbers
I had hoped for.

Am I accessing the text member in my riddle struct inappropriately?

Thanks.
 
I

Ico

anars said:
Hi,

I have this struct initialized globally:
struct riddle {
char *text;
....
}

below, the function I call in main with the riddle struct as arg. is
defined:
void prepareRiddle(struct riddle *riddle)
{
// here I get a string (char *tmp) with a known size (int length)
incl. terminator.
riddle->text = (char*)malloc(length * sizeof(char)); // figured
sizeof(char) could be != 1 on some platforms
....
}

still, sizeof(riddle.text) called in main() doesn't give me the numbers
I had hoped for.

You probably want to use the strlen() function instead of the sizeof()
operator. sizeof() gives you the size of the object, which is in this
case not the text of the string, but a pointer to char.
 
A

anars

Ahh, thank you very much.

Btw, I used the Gmail Groups online poster for this post, so sorry if
I'm top-posting. :) Just testing this thing out.
 
I

Ico

anars said:
Ahh, thank you very much.

You're welcome.
Btw, I used the Gmail Groups online poster for this post, so sorry if
I'm top-posting. :)

Well, it seems you are neither top- or bottom posting, since your last
post did not include any context. Try to quote the relevant parts of the
message(s) you are replying to, preferrably putting your answers under
or inbetween the previous post.
Just testing this thing out.

There are some newsgroups especially ment for testing purposes,
(alt.testing.*), feel free to test as much as you want there.
 
F

Flash Gordon

anars said:
Hi,

I have this struct initialized globally:
struct riddle {
char *text;
....
}

below, the function I call in main with the riddle struct as arg. is
defined:
void prepareRiddle(struct riddle *riddle)
{
// here I get a string (char *tmp) with a known size (int length)
incl. terminator.

Please don't use // style comments on Usenet. As you can see above, they
don't survive line wrapping.
riddle->text = (char*)malloc(length * sizeof(char)); // figured

Don't cast the return value of malloc. It isn't required and can mask
sizeof(char) could be != 1 on some platforms

sizeof(char) is 1 by definition. However, a char (and a byt) can have
more than 8 bits. So you never need to multiply by sizeof(char).
....
}

still, sizeof(riddle.text) called in main() doesn't give me the numbers
I had hoped for.

sizeof will tell you the size of the pointer, not the size of the string
it points to. Imagine this, point at your house with a small stick. Then
point at the stick ans ask someone, "how big is this?" You will get an
answer something like, "1 foot long" rather than the size of your house.
Am I accessing the text member in my riddle struct inappropriately?

Since you have a problem you are obviously doing things wrong. However,
you have not provided a complete example program showing the problem, so
there are probably things wrong other than what I have suggested.

In future, please always provide a small complete compilable program
showing your problem (compilable is optional if your problem is that the
program won't compile!). Copy and paste it in to the message, don't
re-type it, or we won't know what errors are copying errors and what are
errors in your real code.
 
D

Default User

anars said:
Ahh, thank you very much.

Btw, I used the Gmail Groups online poster for this post, so sorry if
I'm top-posting. :) Just testing this thing out.

1. I don't think you know what "top-posting" means. That means posting
your reply above quoted material. As you didn't quote anything, it
would impossible to top (or bottom) post.

2. You must include quotes. See below for information.

3. It's Google Groups.



Brian
 
K

Keith Thompson

Ico said:
You probably want to use the strlen() function instead of the sizeof()
operator. sizeof() gives you the size of the object, which is in this
case not the text of the string, but a pointer to char.

strlen() isn't necessarily the solution either. There's no indication
that the allocated object that riddle->text points to is used to hold
a string (i.e., a sequence of character terminated with '\0').

If you want to know how much space was allocated by malloc(), you need
to remember it. For example:

struct riddle {
char *text;
size_t length;
...
}

...

struct riddle *riddle;
riddle->text = malloc(length);
if (riddle->text == NULL) {
/* handle error */
}
riddle->length = length;
 
R

Richard Bos

Default User said:
1. I don't think you know what "top-posting" means. That means posting
your reply above quoted material. As you didn't quote anything, it
would impossible to top (or bottom) post.

2. You must include quotes. See below for information.

3. It's Google Groups.

No, it's fscking not! It's Usenet; these are newsgroups. Google's
insistent lies that they are Google Groups lies at the base of a whole
lot of aggro caused by Google Groups users for other, more RFC-compliant
netizens.

Richard
 
K

Keith Thompson

No, it's fscking not! It's Usenet; these are newsgroups. Google's
insistent lies that they are Google Groups lies at the base of a whole
lot of aggro caused by Google Groups users for other, more RFC-compliant
netizens.

Richard, I think you misunderstood Default User's point.

"I used the Gmail Groups ...". Default User correctly
pointed out that the name of the interface is "Google Groups", not
"Gmail Groups". The word "It" in "It's Google Groups" referred to the
interface being used, not to Usenet.
 
D

Default User

Richard said:
No, it's fscking not! It's Usenet; these are newsgroups.

Jeez Richard. I've only been posting here for like 12 years, before
Google Groups even existed. You managed to completely miss the entire
point. I've winnowed it down for you.




Brian
 
A

anars

A big thank you to all of you.

Especially you guys who gave hints and guidelines on posting on Usenet
(and newsgroups in general), and of course to Flash Gordon for the
technical insight.

Regards,
Anders
 
R

Richard Bos

Default User said:
Jeez Richard. I've only been posting here for like 12 years, before
Google Groups even existed. You managed to completely miss the entire
point. I've winnowed it down for you.

My apologies. It must be Google Groups users getting on my tits, but
that doesn't excuse my sloppy reading.

Richard
 
D

Default User

Richard said:
My apologies. It must be Google Groups users getting on my tits, but
that doesn't excuse my sloppy reading.


I understand. At least this group tries to deal with the Google
situation, and definitely for the better. Over on comp.lang.c++, the
no-quoting is rampant, and they're just now starting to pipe up about
it.




Brian
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top