473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

string compare

Hi guys,

I am using custom string structures in a project.

typedef struct{
short int length;
char data[256];
}my_long_string;

and

typedef struct{
short int length;
char data[32];
}my_short_string;

I want to create string processing functions like strcmp, strcpy etc
for these types.

While I can create different functions for these two types, is there
any way to use same function to handle both types ??
With C++ this would have been easy ... I'd have to just overload a
function, but since function overloading is not supported in C is
there a way/technique which I can use to simulate similar behaviour ??

regards

Rohin
Jan 23 '08 #1
6 2332
yeti wrote:
Hi guys,

I am using custom string structures in a project.

typedef struct{
short int length;
char data[256];
}my_long_string;

and

typedef struct{
short int length;
char data[32];
}my_short_string;

I want to create string processing functions like strcmp, strcpy etc
for these types.

While I can create different functions for these two types, is there
any way to use same function to handle both types ??
With C++ this would have been easy ... I'd have to just overload a
function, but since function overloading is not supported in C is
there a way/technique which I can use to simulate similar behaviour ??

regards

Rohin
You can download such a library from the lcc-win download
site. It uses the proposed extensions of lcc-win for the C language.

That string library features Strcmp, Strcat, etc.

Source code is included
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jan 23 '08 #2

"yeti" <ro********@gmail.comwrote in message
>
I am using custom string structures in a project.

typedef struct{
short int length;
char data[256];
}my_long_string;

and

typedef struct{
short int length;
char data[32];
}my_short_string;

I want to create string processing functions like strcmp, strcpy etc
for these types.

While I can create different functions for these two types, is there
any way to use same function to handle both types ??
With C++ this would have been easy ... I'd have to just overload a
function, but since function overloading is not supported in C is
there a way/technique which I can use to simulate similar behaviour ??
Sadly no.
You are creating an N squared problem as you add more string types. Ypu can
get round it by converting everything to an intermediate type, but then you
will lose the speed that was the motive for the new types in the first
place.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jan 23 '08 #3
yeti wrote:
On Jan 23, 11:48 am, santosh <santosh....@gmail.comwrote:
>yeti wrote:
>>Hi guys,
I am using custom string structures in a project.
typedef struct{
short int length;
char data[256];
}my_long_string;
and
typedef struct{
short int length;
char data[32];
}my_short_string;
....
>... I would make the types dynamic to start with, so that multiple types
can be avoided. Then the array can grow as needed.
Creating the types dynamic would put in problems like memory leaks. I
don't think it would be safe.
I think Santosh was suggesting a structure approach like this :-

typedef struct {
short length;
char data[]; /* or "char data[1];" if c99 support isn't available */
} my_string;

so if you had a string of 32 characters to work with, you'd do something
like:-

my_string *string_pointer = malloc(sizeof(short) + 32);
/* check the return value */

string_pointer->length = 32;
memcpy(string_pointer->data,<some source>,32);

I'm not sure what risks you perceive in this approach.
Jan 23 '08 #4
jacob navia wrote, On 23/01/08 08:24:
yeti wrote:
>Hi guys,

I am using custom string structures in a project.

typedef struct{
short int length;
char data[256];
}my_long_string;
<snip>
You can download such a library from the lcc-win download
site. It uses the proposed extensions of lcc-win for the C language.

That string library features Strcmp, Strcat, etc.

Source code is included
Note that as it relies on an extension which is, to the best of my
knowledge, unique to lcc-win you will only be able to use the library if
you are prepared to restrict yourself to lcc-win.
--
Flash Gordon
Jan 23 '08 #5
yeti wrote:
Hi guys,

I am using custom string structures in a project.

typedef struct{
short int length;
char data[256];
}my_long_string;

and

typedef struct{
short int length;
char data[32];
}my_short_string;

I want to create string processing functions like strcmp, strcpy etc
for these types.
If offsetof(my_long_string, data) equals offsetof(my_short_string, data)
you can do:

int my_strcmp(const void *a, const void *b)
{
short int a_length = *(const short int *)a;
short int b_length = *(const short int *)b;
const char *a_data = (const char *)a + offsetof(my_long_string, data);
const char *b_data = (const char *)b + offsetof(my_long_string, data);
if (a_length < b_length)
return -1;
else if (a_length b_length)
return +1;
else
return memcmp(a_data, b_data, a_length);
}

void my_strcpy(void *target, const void *source)
{
*(short int *)target = *(const short int *)source;
memcpy((char *)target + offsetof(my_long_string, data),
(const char *)target + offsetof(my_long_string, data),
*(const short int *)source);
}
Of course, it causes UB if you try to copy a string larger than the
destination array, but so does the "real" strcpy.

--
Army1987 (Replace "NOSPAM" with "email")
Jan 23 '08 #6
On Thu, 07 Feb 2008 06:51:13 +0000, James Antill wrote:
On Wed, 30 Jan 2008 15:32:54 -0800, jameskuyper wrote:
>James Antill wrote:
>>On Wed, 23 Jan 2008 13:08:14 +0000, James Kuyper wrote:
Mark Bluemel wrote:
typedef struct {
short length;
char data[]; /* or "char data[1];" if c99 support isn't
available */
} my_string;

#include <stddef.h>
...
mystring *string_pointer = malloc(offsetof(my_string, data) +
32);

This is required to be the same as sizeof(mystring),

Citation, please?

6.7.2.1

#16

As a special case, the last element of a structure with more than one
named member may have an incomplete array type; [...] the size of the
structure shall be equal to the offset of the last element of an
otherwise identical structure that replaces the flexible array member
with an array of unspecified length
This states that sizeof(my_string) must be equal to offsetof(struct {
short length;
char data[n];
}, data) for some fixed n, but that might be different from
offsetof(my_string, data).
#17 even gives an example basically identical to what I posted.
#17 even explicitly mentions that the offset of a flexible array member
might differ from the offset of a non-flexible array member.
Feb 7 '08 #7

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

Similar topics

13
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two...
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
6
by: Maileen | last post by:
Hi, I have the following code : Function GetRequestType(ByVal EvDt As String, ByVal StPeriod As String, ByVal EdPeriod As String, ByVal TaskType As String) As Integer Dim strtest As String Dim...
10
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
11
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one...
11
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
1
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.