I am working on a similar program and am getting an error that states 'strcmp' cannot convert parameter 1 from 'const char' to 'const char *'. ( I haven't gotten to the swap yet.) Do you know how I should fix this?

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAXLINE 100
#define N_STRINGS 7

char *change(const char *s);

int main(void)
	char line[MAXLINE]; 
	char line2[MAXLINE];
	const char *s = line;
	const char *p = line2;
	int k;
	int i;
	int j;

		printf("Enter 7 strings to be sorted.\n\n");

		scanf(" %[ a-zA-Z]", line);
		printf("\n\nThe following %d strings will be sorted: \n\n", N_STRINGS);

		printf("%s\n", change(line));
		printf("\nThe sorted list is displayed below: \n\n");	

		strcpy(line2, line);

		for (k = 0; k < 1; k++)
		printf("%s\n", change(line2));

		for (j = 0; j < N_STRINGS; j++)
			for (i = i + 1; i < N_STRINGS; i++)
				if ((strcmp(p[j], p[i])) > 0)
		return 0;		
9 Years
Discussion Span
Last Post by danzona

if ((strcmp(p[j], p)) > 0)

p[j] and p are characters of a string. strcmp expects a string and not a char


Here are a few tips to address the error you are seeing:
Understand that in C a string is an array of chars. So line and line2 are arrays of chars, and s and p are pointers to those arrays.
Because p is a pointer to an array of chars, p[0] is going to be the first char in that array. It is not going to be a string.
strcmp() wants to take two strings, and this is your problem since you are passing two chars.

I don't want to tell you too much about how to write your program because you are going to learn a lot about strings and char arrays by doing this yourself, but think about this:
When you parse out the user's input into 7 individual strings, where are you going to store them? Have you allocated space for the 7 individual char arrays?

Good luck!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.