By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,509 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

strncpy to a char member of struct

P: n/a
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.

Apr 21 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Ico
anars <an************@gmail.com> wrote:
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.
--
:wq
^X^Cy^K^X^C^C^C^C
Apr 21 '06 #2

P: n/a
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.

Apr 21 '06 #3

P: n/a
Ico
anars <an************@gmail.com> wrote:
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.

--
:wq
^X^Cy^K^X^C^C^C^C
Apr 21 '06 #4

P: n/a
anars wrote:
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
two serious errors, failure to #include <stdlib.h> or compiling the code
as C++ (which is a different language).
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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 21 '06 #5

P: n/a
anars wrote:
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

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 21 '06 #6

P: n/a
Ico <us****@zevv.nl> writes:
anars <an************@gmail.com> wrote:
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.


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;

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 21 '06 #7

P: n/a
"Default User" <de***********@yahoo.com> wrote:
anars wrote:
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.


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
Apr 24 '06 #8

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"default User" <de***********@yahoo.com> wrote:
anars wrote:
> 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.


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.

"anars" wrote "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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 24 '06 #9

P: n/a
Richard Bos wrote:
"Default User" <de***********@yahoo.com> wrote:

Btw, I used the Gmail Groups online poster 3. It's Google Groups.


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

Apr 24 '06 #10

P: n/a
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

Apr 24 '06 #11

P: n/a
"Default User" <de***********@yahoo.com> wrote:
Richard Bos wrote:
"Default User" <de***********@yahoo.com> wrote:

Btw, I used the Gmail Groups online poster 3. It's Google Groups.


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.


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

Richard
Apr 25 '06 #12

P: n/a
Richard Bos wrote:
"Default User" <de***********@yahoo.com> wrote:
Richard Bos wrote:
"Default User" <de***********@yahoo.com> wrote:

> Btw, I used the Gmail Groups online poster

> 3. It's Google Groups.

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.


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

Apr 25 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.