473,406 Members | 2,894 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,406 software developers and data experts.

string query

Hi, I've been experimenting further to understand about ordering
strings.
I had changed the code which tested ordered a list of char ( which
worked ) to string
but it's not giving the results I was expected. I was expecting to
have the strings in an increasing order.

Now, I'm getting confused and would appreciate if someone can explain
why I'm getting these results is greatly appreciated. Am I going about
this the correct way ?

struct testRec
{
char name[5];
struct testRec *nextrec;
};

typedef struct testRec records;
typedef records *recordptr;

void addName(recordptr *start, char *item);

int main ()
{
recordptr start = NULL;
char item[5];
.......
gets(item);
addRec(&start, item);
.....
return 0;
}

void addRec(recordptr *start, char *item)
{
recordptr newptr, prevptr, thisptr;
newptr = malloc(sizeof(records));

if(newptr != NULL)
{
strcpy(newptr->name, item);
newptr->nextrec = NULL;

prevptr = NULL;
thisptr = *start;

while (thisptr != NULL)
{
if (item thisptr->name)
{
prevptr = thisptr;
thisptr = thisptr->nextrec;
}
}
if (prevptr == NULL)
{
newptr->nextrec = *start;
*start = newptr;
}
else
{
prevptr->nextrec = newptr;
newptr->nextrec = thisptr;
}
}
}

My output comes as
Enter a record :aa
List is : aa ->
Enter a record : dd
List is : aa -dd ->
Enter a record : cc
List is :aa -dd -cc ->
Enter a record : bb
List is :aa -dd -cc -bd ->

May 29 '07 #1
10 1585
pt*****@gmail.com said:

<snip>
My output comes as
Enter a record :aa
List is : aa ->
Enter a record : dd
List is : aa -dd ->
Enter a record : cc
List is :aa -dd -cc ->
Enter a record : bb
List is :aa -dd -cc -bd ->
How strange. My output comes as:

foo.c:13: warning: function declaration isn't a prototype
foo.c: In function `main':
foo.c:14: `NULL' undeclared (first use in this function)
foo.c:14: (Each undeclared identifier is reported only once
foo.c:14: for each function it appears in.)
foo.c:16: parse error before `...'
foo.c:18: warning: implicit declaration of function `addRec'
foo.c:19: parse error before `...'
foo.c:21: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:24: warning: no previous prototype for `addRec'
foo.c:24: warning: type mismatch with previous implicit declaration
foo.c:18: warning: previous implicit declaration of `addRec'
foo.c:24: warning: `addRec' was previously implicitly declared to return
`int'
foo.c: In function `addRec':
foo.c:26: warning: implicit declaration of function `malloc'
foo.c:26: warning: assignment makes pointer from integer without a cast
foo.c:28: `NULL' undeclared (first use in this function)
Perhaps it would be better to post the code you're actually using.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 29 '07 #2
pt*****@gmail.com wrote:

(fx:snip)
void addRec(recordptr *start, char *item)
(fx:snip)
if (item thisptr->name)
Comparisons of pointers (`item` and `thisptr->name` are both
`char*`) isn't comparison of strings. What's more, here it's
Undefined Behaviour [1].

(fx:announcer) Mr strcmp is trying to friend you ...

[1] Run away! Run away!

--
"It's just the beginning we've seen" - Colosseum, /Tomorrow's Blues/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

May 29 '07 #3
On May 29, 9:11 am, ptq2...@gmail.com wrote:
Hi, I've been experimenting further to understand about ordering
strings.
I had changed the code which tested ordered a list of char ( which
worked ) to string
but it's not giving the results I was expected. I was expecting to
have the strings in an increasing order.

Now, I'm getting confused and would appreciate if someone can explain
why I'm getting these results is greatly appreciated. Am I going about
this the correct way ?

struct testRec
{
char name[5];
struct testRec *nextrec;

};

typedef struct testRec records;
typedef records *recordptr;
[snip]
void addRec(recordptr *start, char *item)
{
recordptr newptr, prevptr, thisptr;
newptr = malloc(sizeof(records));

if(newptr != NULL)
{
[snip]
while (thisptr != NULL)
{
if (item thisptr->name)
Richard's comments notwithstanding, you have a problem in the test
above.

Think of what you are comparing:
item is a pointer
thisptr->name is a string (which, for the purposes of this
comparison can be considered to be a pointer)
Is it your goal to determine if one pointer is numerically greater
than another? Rather, are you not concerned about the relationship
between the strings pointed to by each pointer?

Rewrite this if statement so that it compares the strings, not the
pointers. And, of course, make certain that you provide (or cause to
be provided) the prototype for any function that you use to perform
that string comparison.
{
[snip]

HTH
--
Lew


May 29 '07 #4
pt*****@gmail.com writes:
Hi, I've been experimenting further to understand about ordering
strings.
I had changed the code which tested ordered a list of char ( which
worked ) to string
but it's not giving the results I was expected. I was expecting to
have the strings in an increasing order.

Now, I'm getting confused and would appreciate if someone can explain
why I'm getting these results is greatly appreciated. Am I going about
this the correct way ?
I'm going to comment on your code without actually answering your
underlying question. Your code has a number of problems that you
should fix before you start worrying about why it behaves the way it
does.
struct testRec
{
char name[5];
struct testRec *nextrec;
};

typedef struct testRec records;
typedef records *recordptr;
The use of typedefs for structures is almost always unnecessary. A
lot of programmers do prefer to use typedefs for structure types, I
suppose just to give the structure a one-word name; you can do that if
you like, but it really doesn't accomplish anything. Typedefs for
pointer types, on the other hand, just cause confusion.

I would simply declare the structure type and drop the typedefs. If I
want to refer to the structure type, I'd just refer to
"struct testRec"; if I wanted to refer to a pointer to that type, I's
refer to "struct testRec*".

And why do you call the type "records" when it's a single record?

But if you insist on using a typedef, there's no reason to use a
different name than the one you used for the structure. For example:

typedef struct testRec
{
...
} testRec;

Now you can refer to your type as either "struct testRec" or
"testRec", and to the pointer type as "testRec*".
void addName(recordptr *start, char *item);
Your start parameter is a pointer to a pointer. If you had declared
it as "struct testRec **start" or "testRec **start", that would have
been clearer.
int main ()
"int main(void)" is preferred.
{
recordptr start = NULL;
char item[5];
......
gets(item);
Never use gets(). Think about what happens if I type a long line, and
read question 12.23 in the comp.lang.c FAQ, <http://www.c-faq.com/>.

[snip]

Furthermore, the program you posted is incomplete. You're missing
several required #include directives, and you have "......" in a
couple of places. We can help you much better if you post a complete,
compilable program that illustrates whatever problem you're having.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 29 '07 #5
pt*****@gmail.com wrote:
>
Hi, I've been experimenting further to understand about ordering
strings. I had changed the code which tested ordered a list of
char ( which worked ) to string but it's not giving the results
I was expected. I was expecting to have the strings in an
increasing order.
Consider comparing strings with strcmp (in <strings.h>).

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 29 '07 #6
CBFalconer said:

<snip>
Consider comparing strings with strcmp (in <strings.h>).
Huh? Since when?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 29 '07 #7
In article <xJ******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>Consider comparing strings with strcmp (in <strings.h>).
>Huh? Since when?
Early 1980s. Perhaps you should have asked "until when?", to which
the answer is arguably 1989.

7th edition unix had strcmp(), strcpy(), strlen(), index() etc, but
didn't have either <string.hor <strings.h- presumably you were
supposed to declare the ones that didn't return int yourself, though
it didn't matter since int and char * were the same size.

BSD (probably 4.1, I don't have the manual handy to check) introduced
<strings.hto declare these. Some early System V introduced
<string.hfor the same purpose. Somewhere along the way System V
presumably also renamed index() and rindex(). And it was the System V
versions that were adopted by the ANSI standard.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 29 '07 #8
On 29 May 2007 22:47:05 GMT, ri*****@cogsci.ed.ac.uk (Richard Tobin)
wrote:
>In article <xJ******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>Consider comparing strings with strcmp (in <strings.h>).
>>Huh? Since when?

Early 1980s. Perhaps you should have asked "until when?", to which
the answer is arguably 1989.
There is no standard header called strings.h, not in K&R II, not in
C89, and not in C99. I assume RH didn't feel the need for a smiley
for something so obvious.
>
7th edition unix had strcmp(), strcpy(), strlen(), index() etc, but
didn't have either <string.hor <strings.h- presumably you were
I didn't realize unix was a compiler but its variations from the
standard are also irrelevant, at least in this group.
>supposed to declare the ones that didn't return int yourself, though
it didn't matter since int and char * were the same size.
It certainly does matter. Having the same size is convenient. Having
the correct type is mandatory. Think of code like
strcat(strcpy(large_array_of_char,"abcdef"),"12345 6");
>
BSD (probably 4.1, I don't have the manual handy to check) introduced
<strings.hto declare these. Some early System V introduced
<string.hfor the same purpose. Somewhere along the way System V
presumably also renamed index() and rindex(). And it was the System V
versions that were adopted by the ANSI standard.

Remove del for email
May 30 '07 #9
Richard Heathfield wrote:
CBFalconer said:

<snip>
>Consider comparing strings with strcmp (in <strings.h>).

Huh? Since when?
Yeah, I always have to think twice when using <string.h>
that I don't type <strings.h>. (I have used BSD systems
that had <strings.hin the distant past.)

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
May 30 '07 #10
In article <la********************************@4ax.com>,
Barry Schwarz <sc******@doezl.netwrote:
>>>Consider comparing strings with strcmp (in <strings.h>).
>>>Huh? Since when?

Early 1980s. Perhaps you should have asked "until when?", to which
the answer is arguably 1989.

There is no standard header called strings.h, not in K&R II, not in
C89, and not in C99.
And which of them was published before 1989?
>>7th edition unix had strcmp(), strcpy(), strlen(), index() etc, but
didn't have either <string.hor <strings.h- presumably you were

I didn't realize unix was a compiler but its variations from the
standard are also irrelevant, at least in this group.
Where do you think the C standard came from? Out of thin air?
If you want to know why C is the way it is, you need to understand
its unix history. If you think that's off-topic for this group,
tough.
>>supposed to declare the ones that didn't return int yourself, though
it didn't matter since int and char * were the same size.

It certainly does matter.
Are you incapable of understanding that a comment might be about a
particular historical system? Hint: note the use of the past tense.
>Having the same size is convenient. Having
the correct type is mandatory. Think of code like
strcat(strcpy(large_array_of_char,"abcdef"),"12345 6");
What is your point? You can take that question either generally or
specifically.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 31 '07 #11

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

Similar topics

2
by: Andy | last post by:
I haven't done any web work for over a year and for the life of me I cannot remember how to use a query string in a URL. I want the link to be page.aspx?ID=1 then pass 1 into a query like this ...
8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
3
by: Dr. Oz | last post by:
Hi, I am trying to read in a query string from one page and build a link to another page based on the query string. Here's the code I am using to read in the query string: <script...
10
by: Angus Comber | last post by:
Hello My code below opens a Word document in binary mode and places the data into a buffer. I then want to search this buffer for a string. I tried using strstr but think it stops looking when...
10
by: Onur Bozkurt | last post by:
I'am sending some data by the querystring. But I don't want it to be seen exactly because of security reasons. Is there a way to encrypt it and later decrypt when reading the querystring...? I...
3
by: Miguel Dias Moura | last post by:
Hello, I am calling an ASP.NET / VB as follows: search.aspx?search=asp%20book%20london Then I create a string with the keywords like this: Dim keywords() As String =...
3
by: Jim Lewis | last post by:
I have read several things that state accessing a Web Service through a Query String should work. However, when I try to execute http://localhost/webservice1/service1.asmx/HelloWorld I get the...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
3
bilibytes
by: bilibytes | last post by:
Hi, I am having a problem with a concatenated string. I start my string outside of a for() and then, concatenate the string generated with the loop to it. the string out of the loop looks like...
2
by: Looch | last post by:
All, I'm trying to output but I can only get (brackets for clarity) when using the code below. How can I "break" into the query variable in the InsertName method to add the name parameter to...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
isladogs
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...

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.