M1ndTr1p
M1ndTr1p

Reputation: 23

Using strcmp with Index of Array

I am trying to do coping string to string but

  1. Compiler gives Cannot convert Char to constant char for 15,18 Lines;
  2. Compiler gives Cannot convert Char to Char For 19,20 Lines; Couldn't find what is wrong with this?

    int main () 
    { 
        int i=0; 
        int j=0;
        int space = 0; 
    
        char cmp[50]; 
        char *p[50][100];
    
        for (i=0;i<space;i++) { 
            for ( j = i + 1;j <=space; j++ ) { 
                if( (strcmp(p[i],p[j])=0)) //15 {  
                    strcpy (cmp,p[i]); //18
                    strcpy (p[i],p[j] ); //19
                    strcpy(p[j], cmp); //20
                } 
            }
        }
    }
    

Upvotes: 1

Views: 1453

Answers (2)

Akshay Kulkarni
Akshay Kulkarni

Reputation: 740

Try this code, but let me tell you this code swaps same strings if value of space is some positive number.

int main () 
{

    int i=0; 
    int j=0;
    int space = 0; //I hope you initialized the value of space to a different number.

    char cmp[50]; 
    char p[50][100];// do not use *p[50][100]

    for (i=0;i<space;i++) { 
        for ( j = i + 1;j <=space; j++ ) { 
            if( (strcmp(p[i],p[j])==0))  {  //use == for comparison.
                strcpy (cmp,p[i]); 
                strcpy (p[i],p[j] ); 
                strcpy(p[j], cmp); 
            } 
        }
    }
}

Upvotes: 3

abelenky
abelenky

Reputation: 64682

So many problems.....

for (i=0;i<space;i++) { 

What do you think the value of space is?
if space is zero, how many times do you think this loop will run?
How many times do you want it to run?

Upvotes: 1

Related Questions