473,785 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strncpy to a char member of struct

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(s truct riddle *riddle)
{
// here I get a string (char *tmp) with a known size (int length)
incl. terminator.
riddle->text = (char*)malloc(l ength * sizeof(char)); // figured
sizeof(char) could be != 1 on some platforms
....
}

still, sizeof(riddle.t ext) 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
12 2395
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(s truct riddle *riddle)
{
// here I get a string (char *tmp) with a known size (int length)
incl. terminator.
riddle->text = (char*)malloc(l ength * sizeof(char)); // figured
sizeof(char) could be != 1 on some platforms
....
}

still, sizeof(riddle.t ext) 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
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
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
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(s truct 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(l ength * 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.t ext) 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
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
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(s truct riddle *riddle)
{
// here I get a string (char *tmp) with a known size (int length)
incl. terminator.
riddle->text = (char*)malloc(l ength * sizeof(char)); // figured
sizeof(char) could be != 1 on some platforms
....
}

still, sizeof(riddle.t ext) 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_Keit h) 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
"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
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_Keit h) 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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
17654
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
7
5293
by: Angus Comber | last post by:
Hello Here is my code so far. Is this correct/incorrect/along the right lines/other? #include <stdio.h> #include <string.h> #include <search.h> struct mystruct {
4
4481
by: Barry | last post by:
Hi all! I support a rather large production EDI application with a number of C programs, and I ran across a very interesting problem. I have some code that used to work just fine for years, and now all of a sudden it doesn't work any more. The input to the program did not change at all (even ran it through a binary browser to make certain there were no hidden chars or something strange) and the program has not been recompiled in years....
9
2038
by: fybar | last post by:
Hi, I am in the progress of writing a program that will read in the contents of a directory and then do some stuff to some of the files. So far I am getting the contents of the directory but I get a strange result from one file. Here is my program, feel free to critique: #include <sys/types.h> #include <dirent.h> #include <stdio.h>
53
4293
by: SK | last post by:
Hi I appreciate all of the feedback I have recieved. I am enjoying working on my program and I hope that it can be appreciated. I have my program compiling now and I am continuing to work out the bugs. I have two problems that I cannot seem to resolve: 1. I want to have someone enter in as many employees as they want to..and for each employee entered I would like to save the data from that entry to print out at the end of the...
31
7760
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
4
9539
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char *strcpy(char *s1, const char *s2) // library function
4
5772
by: lurch132002 | last post by:
i am trying to create an array of structs to hold some information but whenever i get to the second element and try to strncpy it i get a segmenation fault. ive searched around for similar problems but i cant seem to figure out what im doing wrong. any help would be appreciated. #include <stdio.h> #include <stdlib.h> #include <string.h>
5
2287
osfreak
by: osfreak | last post by:
#include<iostream> using namespace std; typedef struct mystruct { unsigned char uDirFlag; unsigned int uFileSize; char uFilename;
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.