3) please explain the below c program for sorting

Status
Not open for further replies.

rangerskm

Full Member level 4
Joined
Jan 23, 2013
Messages
199
Helped
0
Reputation
2
Reaction score
0
Trophy points
1,296
Visit site
Activity points
2,663
compiler used=gcc
os -fedora 19
Code:
#include <stdio.h>
   #include <string.h>

   #define MAXLINES 5000     /* max #lines to be sorted */

   char *lineptr[MAXLINES];  /* pointers to text lines */

   int readlines(char *lineptr[], int nlines);
   void writelines(char *lineptr[], int nlines);

   void qsort(char *lineptr[], int left, int right);

   /* sort input lines */
   main()
   {
       int nlines;     /* number of input lines read */

       if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
           qsort(lineptr, 0, nlines-1);
           writelines(lineptr, nlines);
           return 0;
       } else {
           printf("error: input too big to sort\n");
           return 1;
       }
   }

   #define MAXLEN 1000  /* max length of any input line */
   int getline(char *, int);
   char *alloc(int);

   /* readlines:  read input lines */
   int readlines(char *lineptr[], int maxlines)
   {
       int len, nlines;
       char *p, line[MAXLEN];

       nlines = 0;
       while ((len = getline(line, MAXLEN)) > 0)
           if (nlines >= maxlines || p = alloc(len) == NULL)
               return -1;
           else {
               line[len-1] = '\0';  /* delete newline */
               strcpy(p, line);
               lineptr[nlines++] = p;
           }
       return nlines;
   }

   /* writelines:  write output lines */
   void writelines(char *lineptr[], int nlines)
   {
       int i;

       for (i = 0; i < nlines; i++)
           printf("%s\n", lineptr[i]);
   }

i got the error message
 

Why did you create an entirely new thread for the same discussion again. You could have continued it there itself...??

Also the reason for first error has been posted there.

You second error is due to parenthesis mistake , i guess.
It should be like this.
Code:
           if (nlines >= maxlines || ((p = alloc(len)) == NULL))
 
Hi rangerskm ,

[sathish@localhost ~]$ vi pointarr.c
[sathish@localhost ~]$ gcc pointarr.c
pointarr.c:29:8: error: conflicting types for ‘getline’
int getline(char *, int);

I hope its a local and global scope problem
try these..

Code:
   #define MAXLINES 5000     /* max #lines to be sorted */
    #define MAXLEN 1000  /* max length of any input line */
   
   char *lineptr[MAXLINES];  /* pointers to text lines */
      int getline(char *, int);
   char *alloc(int);
   int readlines(char *lineptr[], int nlines);
   void writelines(char *lineptr[], int nlines);

   void qsort(char *lineptr[], int left, int right);

   /* sort input lines */
   main()
   {

}
In file included from pointarr.c:1:0:
/usr/include/stdio.h:678:20: note: previous declaration of ‘getline’ was here
extern _IO_ssize_t getline (char **__restrict __lineptr,

/usr/include/stdio.h in this inbuild libray fuction is already using getline declaration. you have to rename your declaration.

pointarr.c: In function ‘readlines’:
pointarr.c:40:40: error: lvalue required as left operand of assignment
if (nlines >= maxlines || p = alloc(len) == NULL)

p = alloc(len)????

You need to compare, not assign:

About the error:

lvalue required as left operand of assignment

lvalue means an assignable value (variable), and in assignment the left value to the = has to be lvalue (pretty clear).

Both function results and constants are not assignable (rvalues), so they are rvalues. so the order doesn't matter and if you forget to use == you will get this error. (editI consider it a good practice in comparison to put the constant in the left size, so if you write = instead of ==, you will get a compilation error. for example:

Code:
int a = 5;
if (a = 0) // Always evaluated as false, no error.
{
    //...
}

Code:
int a = 5;
if (0 = a) // Generates compilation error, you cannot assign a to 0 (rvalue)
{
    //...
}

update me.

Best regards,
 
If this is correct then


Code C - [expand]
1
int getline(char *, int);



this is wrong


Code C - [expand]
1
(len = getline(line, MAXLEN)) > 0



The first argument is a pointer to a char and it expects address of a char variable in the calling function.

It should be


Code C - [expand]
1
(len = getline(&line, MAXLEN)) > 0



Change


Code C - [expand]
1
if (nlines >= maxlines || p = alloc(len) == NULL)



to


Code C - [expand]
1
if ((nlines >= maxlines) || (p = alloc(len) == NULL))

 

i tried the code by making changes .but its shows still error

Code:
[sathish@localhost ~]$ gcc sort1.c
/tmp/ccZ1YzHU.o: In function `readlines':
sort1.c:(.text+0x96): undefined reference to `alloc'
sort1.c:(.text+0xf9): undefined reference to `gettline'
collect2: error: ld returned 1 exit status
 

The function to allocate memory is malloc/calloc , not simply alloc.

And the second error will be due to some typo mistake. You may have forgot to change the name of the function (adding an extra 't' ) somewhere in the program.
Just check it and update.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…