Hi,
I am a bit confused with zero based items and strncpy(...)
assuming I have
char str1[] = "Hello world";
char str2[3];
I now have str2 that should look something like
str[0] == '?';
str[1] == '?';
str[2] == '?';
where '?' is any character that was there b4
and str2 does not have str[3] == '?';
am I right so far?
now can I also say that if I did
int nSize = strlen(str2);
I could get any value really 'cause there is no null char
now if I wanted to create a function to get 2 characters from a string I
could do something like..
the reason why I want a function is because the size(s) might change at a
later stage so I'd rather change one function and all the strings that call
it.
I also want to make sure that the data has a terminating null char and that
the rest of the data is set to null as well.
int MyCopy( char szDest[3], const char*szSource )
{
memset( szDest, 0, 3 );
strcpy( szDest, szSource, 2 );
szDest[2] = '\0';
strlen(szDest );
}
is my logic above correct? or would it be better to define my function as
(but I would I ensure that the size is correct?
int MyCopy( char *szDest, const char*szSource )
{
...
}
many thanks in advance.
regards. 3 2222
"Simon" <sp********@sch oolsofafrica.co m> wrote in message
news:2q******** ****@uni-berlin.de... Hi,
I am a bit confused with zero based items and strncpy(...)
assuming I have
char str1[] = "Hello world"; char str2[3];
I now have str2 that should look something like
str[0] == '?'; str[1] == '?'; str[2] == '?';
where '?' is any character that was there b4
and str2 does not have str[3] == '?';
am I right so far? now can I also say that if I did
int nSize = strlen(str2); I could get any value really 'cause there is no null char
I think the situation is 'undefined'.
now if I wanted to create a function to get 2 characters from a string I could do something like.. the reason why I want a function is because the size(s) might change at a later stage so I'd rather change one function and all the strings that call it. I also want to make sure that the data has a terminating null char and that the rest of the data is set to null as well.
int MyCopy( char szDest[3], const char*szSource ) { memset( szDest, 0, 3 ); strcpy( szDest, szSource, 2 ); szDest[2] = '\0';
strlen(szDest ); }
My problem with using a function like that are the design ramifications.
Hardcoding those kind of values is silly at best. :P
is my logic above correct? or would it be better to define my function as (but I would I ensure that the size is correct?
int MyCopy( char *szDest, const char*szSource ) { ... }
This would be fine assuming you zero terminated each passed string. Or why
not implement a 'limit' copy. Pass a number indicated the size. Or better
yet, use a supplied system function (most of the string and printf functions
have buffer 'safe' counterparts) for the desired results.
many thanks in advance.
regards.
"Simon" <sp********@sch oolsofafrica.co m> wrote in message
news:2q******** ****@uni-berlin.de... int MyCopy( char szDest[3], const char*szSource ) { memset( szDest, 0, 3 ); strcpy( szDest, szSource, 2 );
~~~~~~ strncpy szDest[2] = '\0';
strlen(szDest ); }
is my logic above correct? or would it be better to define my function as (but I would I ensure that the size is correct?
The function could be simplified to:
int MyCopy( char szDest[3], const char*szSource )
{
strncpy( szDest, szSource, 2 );
szDest[2] = '\0';
return strlen(szDest );
}
And if your library provides as an extension the safer strlcpy
function that was introducted by the OpenBSD team (see http://www.gsp.com/cgi-bin/man.cgi?s...&topic=strlcpy ),
you can even write:
int MyCopy( char szDest[3], const char*szSource )
{
strlcpy( szDest, szSource, 3 );
return strlen(szDest);
}
.... and actually get rid of the function altogether.
This said, people in this NG prefer to use std::string ;)
Cheers,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
> > is my logic above correct? or would it be better to define my function
as (but I would I ensure that the size is correct? The function could be simplified to: int MyCopy( char szDest[3], const char*szSource ) { strncpy( szDest, szSource, 2 ); szDest[2] = '\0';
return strlen(szDest ); }
I understand, but am I right in assuming there is nothing wrong with adding
memset( szDest, 0, 3 ); even if it is a waste of space. And if your library provides as an extension the safer strlcpy function that was introducted by the OpenBSD team (see http://www.gsp.com/cgi-bin/man.cgi?s...&topic=strlcpy ), you can even write:
no i do not seem to have it.
This said, people in this NG prefer to use std::string ;)
of course, but I am playing with chars for now :) Cheers, Ivan
Simon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Scott Navarre |
last post by:
Hello,
I have noticed that when using JavaScript to create a page, the sizes of
the text input form elements come out bigger than the size I am trying to
set them to. I don't know if it matters, but I am using IE6...
For example: if I create a text input with a size of "1" using straight
HTML, and then type into it, I can see 2 characters at a time. But when I
use JavaScript's 'document.write()' function to create the same text...
|
by: Harry_Crow |
last post by:
I want to restrict the minimum size of the form. I find that there are
properties MaximumSize and MinimumSize. This will help you control the
maximum and minimum size the form, when the form is resized.
but I find using this will make the controls on the form to flicker more.
one can even restrict using WM_SIZING message but the position of the
window is changing slowly while doing so.
does anyone know the logic how the window minimum size...
|
by: luc h |
last post by:
Hello!
I'm trying to make a very simple wizard. I have 3 different property
pages and the property sheet is set to be a wizard.
The problem is the size of the wizard dialog which is much too big. I
thought I would be the size of the biggest property page plus some
space for the next and previous buttons if needed.
Are you aware of any minimum size for a wizard?
|
by: Ron Vecchi |
last post by:
I know the math I need to perform on width and height to keep an aspect
ratio but where and how would I implement keeping a set aspect ratio on a
form when a user resizes it. Override OnResize? couldn't quite figure it
out.
--
Ron Vecchi
|
by: David Sworder |
last post by:
Hi,
I'm writing an application in which a client (C#/WinForms) and server
(C#/service) interact with one another. The client establishes a "session"
with the server but for scalability reasons there is not a one to one map
between a session and a physical TCP connection. A client may disconnect the
TCP connection if it is idle for more than 60 seconds... yet a conceptual
"session" may last for days at a time.
It's necessary that the...
| |
by: David Douglass |
last post by:
I'm confused about the program below. Based on my reading of the C# spec, I
don't think it should compile, but it does when using Beta 1. Could somebody
please explain the function selection rules to me?
Thanks,
--
David Douglass
MCSD for Microsoft .NET
===================================================
#region Using directives
|
by: cpp_novice |
last post by:
What is the minimum size of 'long' according to the ANSI 1990 standard?
I thow that c99 requires longs to be atleast 32bits wide.
Is there an electronic copy of the 1990 standard available online?
|
by: MLM450 |
last post by:
I am using CWinFormsView to host my C# control. When I resize the view
window to a larger size the control seems to grow with it just fine.
But when I shrink the size of the view, I get scroll bars. I don't want
scrollbars, I want the control to shrink.
In the .NET control, I have tried adjusting the minimum size and the
auto size mode properties. In my view, I have tried setting the
controls size when the view is resized. None of this...
|
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>
|
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...
|
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...
| |
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |