S
Simon
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.
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.