Reputation: 31
I just started using pointers in c. I thought to sort a string by entering the string in the command prompt.
When I try to implement it, I get this error:
'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
Could you please guide me and tell me how to make the code with less pointer and more of array?
char *sort(char *sortIt)
{
char *p =sortIt;
//p = (char*)calloc(sizeof(char));
char temp[3];
int len = strlen(sortIt);
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(strcmp(p[i],p[j])>0)
{
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
for(int ij=0;ij<len;ij++)
printf("%s", p[ij]);
}
int main(char argc, char **argv)
{
char *p=argv[1];
sort(p);
}
Upvotes: 0
Views: 5191
Reputation: 153348
It appear code is attempting to sort the char
in a string. Corrections and ideas noted in code.
char *sort(char *sortIt)
{
// After assignment p & sortIt never change. Only need one of these.
char *p =sortIt;
// casting the return of callloc() and malloc() is frowned upon.
// Note: sizeof(char) is _always_ 1
//p = (char*)calloc(sizeof(char));
char temp[3]; // Only 1 char is needed. Best to declare in inner lock
int len = strlen(sortIt); // Better to use size_t len,i,j,ij;
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
// Only need to compare p[i] > p[j]
if(strcmp(p[i],p[j])>0)
{
// Only need to swap p[i] p[j]
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
// Only need to print the string once
for(int ij=0;ij<len;ij++)
printf("%s", p[ij]);
// missing return
}
Sample solution:
char *sort(char *p) {
size_t len = strlen(p);
for (size_t i = 1; i < len; i++) {
for (size_t j = 0; j < i; j++) {
/// As this code changed the order of indexing, maybe p[i] < p[j]
if (p[i] > p[j]) {
char temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
printf("%s\n", p); // Recommend adding \n
return p;
}
[Edit]
I see trouble with the original main()
code. Below is code with suggested changes:
int main(char argc, char **argv)
{
// Good to test if argv[1] is valid.
if (argc <= 1) return 1;
// Although legal for historic reasons, best to
// char *p=argv[1];
const char *p=argv[1];
// Since sort() is going to re-arrange p, either sort() allocates new memory
// or we allocate memory here. Then const char *p is not needed.
size_t size = strlen(argv[1]) + 1;
char *q;
q = malloc(size);
memcpy(q, argv[1], size);
// sort(p);
sort(q);
// Good policy to free allocated memory
free(q);
return 0; // Always good to return a value from main()
}
Upvotes: 0
Reputation: 373
EDIT:
Change all the
p[i], p[j]
to
&p[i], &p[j]
because you want the const char pointers. In your original case, you will be just looking at chars.
Upvotes: 0
Reputation: 11047
In C, strings mean null terminated strings. Therefore most C library functions take advantage of that. For example, strcmp
, strcpy
, etc.
So your code is wrong in calling strcmp(p[i],p[j])
and strcpy(temp,p[i])
because strcmp
and strcpy
deal with strings
not chars
and p[i]
is a char.
To compare chars in C, you can just use if (p[i] > p[j]) ...
. And for copy a char you can just char c = p[j];
.
Upvotes: 1