0

I am having troubles to finish my program i got some errors and i don't know what to d to fix them. My mission is to build a program that will get from the user 2 strings. i have to print them, then to sort them after that to make a function that will merge and sort with two strings into one. But if a name appears in the first string and second string then the third is the union of both is present in sorted order the two strings and display only once the name that will not be duplicates I guess it's about the malloc memory for the strings but i don't really know what to do!

1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(46): error C2106: '=' : left operand must be l-value
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(47): error C2106: '=' : left operand must be l-value
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

this is my code:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define LINES 4
#define length 100

void SortText1(char text1[LINES][length]);
void SortText2(char text2[LINES][length]);
void PrintStr1(char text1[LINES][length]);
void PrintStr2(char text2[LINES][length]);
void main ()
{
    int i;
    char text1[LINES][length];
    char text2[LINES][length];
    char text3[LINES+LINES][length];
    printf("First string,Please enter %d names (only capital letters,without spaces):", LINES);
    for ( i=0 ; i<LINES ; i++)
    {
        gets(text1[i]);
    }
    printf("Second string,Please enter %d names (only capital letters,without spaces):", LINES);
    for ( i=0 ; i<LINES ; i++)
    {
        gets(text2[i]);
    }
    PrintStr1(text1);
    PrintStr2(text2);
    SortText1(text1);
    system("pause");
    return ;
}
void SortText1(char text1[LINES][length])
{

    int i,j,k;
    for (i=LINES-1 ; i>0 ; i--)
    {
        for ( j=0 ; j<i ; j++ )
        {
            if(strcmp(text1[i],text1[i+1]) > 0 )
            {
                char *temp=text1[i+1];
                text1[i+1]=text1[i];
                text1[i]=temp;
            }
        }
    }
    printf("The first string sorted:\n");
    for ( k=0 ; i<LINES ; k++)
        {
            printf("%s ",text1[k]);
        }
    printf("\n");
}
void PrintStr1(char text1[LINES][length])
{
    int i;
    printf("The first string of NAMES:\n");
    for ( i=0 ; i<LINES ; i++)
        {
            printf("%s ",text1[i]);
        }
    printf("\n");
}
void PrintStr2(char text2[LINES][length])
{
    int i;
    printf("The second string of NAMES:\n");
    for ( i=0 ; i<LINES ; i++)
        {
            printf("%s ",text2[i]);
        }
    printf("\n");
}
2
  • Which lines are 46 and 47 (the ones mentioned in the errors)? Commented Sep 19, 2013 at 16:42
  • Those lines text1[i+1]=text1[i]; text1[i]=temp; Commented Sep 19, 2013 at 18:01

3 Answers 3

2

You need to use strcpy function to copy string

Fix Line No. 45 & 46

    if(strcmp(text1[i],text1[i+1]) > 0 )
    {
        char *temp= malloc(sizeof(char)*strlen(text1[i+1])+1);
        strcpy(temp,text1[i+1]);
        strcpy(text1[i+1],text1[i]); // Line No. 45
        strcpy(text1[i],temp); //Line No. 46
        free(temp);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

No need to dnyamically allocate temp, as the source is never going to be longer than length character you can just declare it as a normal array or length characters.
0

You will not be able to sort it like that since you have a 2-dim array so this code is not correct for that purpose

if(strcmp(text1[i],text1[i+1]) > 0 )
{
  char *temp=text1[i+1];
  text1[i+1]=text1[i];
  text1[i]=temp;
}

if you want to swap two arrays with another you need to physically copy the characters from one array to the other e.g. strncpy.

A more effective way is to create an array of pointers to strings and then swap the pointers.

char *text1[SIZE];
for (i = 0; i < SIZE; ++i)
  text1[i] = malloc(length);

...

fgets( text1[i], length, stdin );

...

then your swap functionality would work

if(strcmp(text1[i],text1[i+1]) > 0 )
{
  char *temp=text1[i+1];  
  text1[i+1]=text1[i];     
  text1[i]=temp;
}

3 Comments

Hey claptrap what does fgets means? In the main I should do it after i create the array?
Sorry for my ignorance. Newcomer I'd appreciate it if you show me the boot memory for the array and a pointer definition
fgets is safer than gets which does not give you a possibility to tell a max size of the buffer so with gets you could potentially enter a too long string and then get an error.
0

You need to understand what an lvalue is. It's quite an important concept in C.

Basically, an lvalue is a value you can assign a value to, like a variable.

This line:

text1[i+1]=text1[i]

tries to assign a whole row of values at once. text1[i + 1] is not an lvalue so the statement is illegal.

It's better to define your data structures as arrays of strings (char*) e.g.

char* text1[LINES];

But you do need to do a bit more set up to allocate and initialise the memory.

2 Comments

Sorry for my ignorance. Newcomer I'd appreciate it if you show me the boot memory for the array and a pointer definition
I am still getting errors while i trying to set up memory allocte in the main.void main () { int i; char text1[LINES][length]; char text2[LINES][length]; char text3[LINES+LINES][length]; printf("First string,Please enter %d names (only capital letters,without spaces):", LINES); for ( i=0 ; i<LINES ; i++) { gets(text1[i]); } for (i = 0; i<LINES ; i++) { text1[i]=(char )malloc(lengthsizeof(char)); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.