Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,671 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,184 people online right now. Registration is fast and FREE... Join Now!




C programming

 
Reply to this topicStart new topic

C programming, strcmp error 2664

sjshark11
post 16 Jul, 2007 - 11:59 AM
Post #1


New D.I.C Head

*
Joined: 16 Jul, 2007
Posts: 1


My Contributions


CODE
#include <stdio.h>
#include <string.h>
void insertion_sort(const char *a, int size);
#define SIZE 4

main()
{
const char *array[SIZE] = { "This", "Is", "Silicon", "Valley" };
printf("%s%s%s%s", array[0], array[1], array[2], array[3]);


insertion_sort(*array, SIZE);
printf("%s%s%s%s", array[0], array[1], array[2], array[3]);
return 0;
}

void insertion_sort( const char *a, int size)
{
int COUNT, IND;

for(COUNT = 1; COUNT <= size-1; COUNT++){
IND = COUNT;

[u]if((IND >= 1) &&((strcmp (a[IND-1], a[IND])) > 0))[/u]
{
printf("%s", a[IND-1]);
IND--;
}

}
}


"I got that error C2664: 'strcmp' : cannot convert parameter 1 from 'const char' to 'const char *'" also. Can anyone tell me why I got this message?? The error line is underlined!
User is offlineProfile CardPM

Go to the top of the page

enpey
post 16 Jul, 2007 - 06:31 PM
Post #2


D.I.C Head

**
Joined: 2 May, 2007
Posts: 59



Thanked 1 times
My Contributions



Why in the last line of your if block with the strcmp, do you have IND-- ? Won't cause a problem, but a purposeless line nonetheless. IND will equal COUNT at the beginning of your next for loop.

Sorry, in relation to your problem, why don't you try using

CODE


if((IND >= 1) && (a[IND-1] != a[IND]) )



enpey
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2007 - 04:12 AM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


The non-equality operators and equality operators can be used with individual characters, or with string objects, but for C style strings, strcmp must be used for a proper comparison.
User is online!Profile CardPM

Go to the top of the page

boz.le.fou
post 17 Jul, 2007 - 09:13 AM
Post #4


New D.I.C Head

*
Joined: 22 Jan, 2007
Posts: 3


My Contributions


Hi sjshark11,
I am not sure that i understood what you wanted to compare.
If you want to compare strings i think your insertion_sort is not well defined
you should declare it as follow :
CODE

void insertion_sort(const char *a[], int size);


That give the following source code.

I agree with enpey, i also think that your IND=COUNT inside the FOR is a mistake.

CODE

#include <stdio.h>
#include <string.h>
void insertion_sort(const char *a[], int size);
#define SIZE 4

main()
{
  int res;
const char *array[SIZE] = { "This", "Is", "Silicon", "Valley" };
printf("%s%s%s%s\n", array[0], array[1], array[2], array[3]);


insertion_sort(array, SIZE);


printf("%s%s%s%s\n", array[0], array[1], array[2], array[3]);
return 0;
}

void insertion_sort( const char *a[], int size)
{
int COUNT, IND;

for(COUNT = 1; COUNT <= size-1; COUNT++){
IND = COUNT;

if((IND >= 1) &&((strcmp ((a[IND-1]), (a[IND]))) > 0))
{
printf("%s\n", a[IND-1]);
IND--;
}
}
}


------------------------------------------------
Coding is the worst job i have ever seen. you spend a lot of energy to debug and you're never proud of the solution smile.gif
Me 2005

This post has been edited by boz.le.fou: 17 Jul, 2007 - 09:18 AM
User is offlineProfile CardPM

Go to the top of the page

ericode
post 17 Jul, 2007 - 09:33 PM
Post #5


D.I.C Head

Group Icon
Joined: 9 Dec, 2006
Posts: 89



Dream Kudos: 75
My Contributions


The answer to your question is that you made your insertion_sort method header wrong. Yes your strcmp would work fine if you were running it on the 'array' variable, but you are running it on the 'a' parameter which is of a different type. Below is your code modified to the point that it will compile (whether or not the method works, that was not your original question...):

CODE

#include <stdio.h>
#include <string.h>
//************CHANGED**************************
void insertion_sort(const char **a, int size);
//*********************************************
#define SIZE 4

main()
{
const char *array[SIZE] = { "This", "Is", "Silicon", "Valley" };
printf("%s%s%s%s", array[0], array[1], array[2], array[3]);

//**********CHANGED************************
insertion_sort(array, SIZE);
//*****************************************
printf("%s%s%s%s", array[0], array[1], array[2], array[3]);
return 0;
}

//***************CHANGED***********************
void insertion_sort( const char **a, int size)
//***********************************************
{
int COUNT, IND;

for(COUNT = 1; COUNT <= size-1; COUNT++){
IND = COUNT;


if((IND >= 1) &&((strcmp (a[IND-1], a[IND])) > 0))
{
printf("%s", a[IND-1]);
IND--;
}

}
}




QUOTE(sjshark11 @ 16 Jul, 2007 - 12:59 PM) *

#include <stdio.h>
#include <string.h>
void insertion_sort(const char *a, int size);
#define SIZE 4

main()
{
const char *array[SIZE] = { "This", "Is", "Silicon", "Valley" };
printf("%s%s%s%s", array[0], array[1], array[2], array[3]);


insertion_sort(*array, SIZE);
printf("%s%s%s%s", array[0], array[1], array[2], array[3]);
return 0;
}

void insertion_sort( const char *a, int size)
{
int COUNT, IND;

for(COUNT = 1; COUNT <= size-1; COUNT++){
IND = COUNT;

if((IND >= 1) &&((strcmp (a[IND-1], a[IND])) > 0))
{
printf("%s", a[IND-1]);
IND--;
}

}
}

"I got that error C2664: 'strcmp' : cannot convert parameter 1 from 'const char' to 'const char *'" also. Can anyone tell me why I got this message?? The error line is underlined!

User is offlineProfile CardPM

Go to the top of the page

enpey
post 17 Jul, 2007 - 10:53 PM
Post #6


D.I.C Head

**
Joined: 2 May, 2007
Posts: 59



Thanked 1 times
My Contributions


My apologies for the above mentioned error. May I ask why it is necassary to use two asterisks here in the header:
CODE

void insertion_sort( const char **a, int size)
User is offlineProfile CardPM

Go to the top of the page

boz.le.fou
post 18 Jul, 2007 - 12:41 AM
Post #7


New D.I.C Head

*
Joined: 22 Jan, 2007
Posts: 3


My Contributions


QUOTE(enpey @ 17 Jul, 2007 - 11:53 PM) *

My apologies for the above mentioned error. May I ask why it is necassary to use two asterisks here in the header:
CODE

void insertion_sort( const char **a, int size)



"char **a" is the declaration of a double pointer declaration
To obtain the value the program will need two steps of redirection.

"a" give the adress to go to "*a" that gives the adress to go reading the value to in "**a" which the case that interest us in this example.
We can notice that "*a[]" and "**a" are equivalent, "*a[]" is may be more understanding for the humankind but the compiler and the computer don't make any difference.
wink2.gif



User is offlineProfile CardPM

Go to the top of the page

skullhackwolverine
post 18 Jul, 2007 - 09:10 AM
Post #8


New D.I.C Head

*
Joined: 18 Jul, 2007
Posts: 1


My Contributions


QUOTE(enpey @ 16 Jul, 2007 - 07:31 PM) *

Why in the last line of your if block with the strcmp, do you have IND-- ? Won't cause a problem, but a purposeless line nonetheless. IND will equal COUNT at the beginning of your next for loop.

Sorry, in relation to your problem, why don't you try using

CODE


if((IND >= 1) && (a[IND-1] != a[IND]) )



enpey


this command can be used only for the integer values or datatypes other than string,character. it can also be used for comparing expressions
User is offlineProfile CardPM

Go to the top of the page

madgundam
post 19 Jul, 2007 - 03:31 PM
Post #9


New D.I.C Head

*
Joined: 19 Jul, 2007
Posts: 2


My Contributions


Try using
CODE
insertion_sort(array, SIZE);

rather than using
CODE
insertion_sort(*array, SIZE);

in the main()
plus whatever insertion logic you are using which is beyond my grasp
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:03AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month