Link to home
Start Free TrialLog in
Avatar of pyramid
pyramid

asked on

null return comes up on screen

Hi C gods

I have a program the does the following.

Input a list of string from the keyboard
sort them using bubble sort method
and display them on the screen;

Example:
If I put in the following :

Mable
Able
notAbble

I should get
Able
mable
notable

Instead I get
Able
(null)
notable

For some reason the blank line I entering being interpreted as part of the sort sequence and is kicking out one of the string.

Anybody see this before..

Thanks in advance
P..
Avatar of jkr
jkr
Flag of Germany image

This is usually the result of sth. like

printf ( "%s\n", NULL);

But as you didn't show any code, it's hard to tell what causes the NULL pointer...
if you attempt to printf a char * that aint been malloced you get that.
Avatar of kishore_joshi
kishore_joshi

While reading the strings, if u r reading them in a loop and if u have given a \n character in the scanf() function then u may get this NULL value.

for(... ; .... ; ....)
{
   ........
   ........
   scanf("%s\n");          /// please remove this \n and try
}

If this is not the reason, then please post u r code and then we may be able to help u.
 Bye,
Kishore
Avatar of pyramid

ASKER

Ok. heres the code. The format is tight due to the limited window given me.

/* Program the reads text as lines from the keyboard, sort it and lists it */
/* alphabetically using functions */
/* There is an error in this program it stores the word "null" in the buffer/*/
/* it prints out the word "null" and kick out one of the sorted words */

/* INCLUDE STATEMENTS AND PREPROCESSOR DEFINITIONS */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLINES 25
/*************************************************************************/
/* PROTOTYPES */

int get_lines(char *line[]);
void sort(char *p[], int n);
void print_strings(char *p[], int n);
/************************************************************************/
/* DEFINE VARIABLE FOR ARRAY WITH POINTER = 25 LINES TO BE SORTED */

int *lines[MAXLINES];
/************************************************************************/

main()
{
int number_of_lines;

             /* Read the lines from the keyboard */

        number_of_lines = get_lines(lines);

       if (number_of_lines < 0)
       {
       puts("Memory Allocation Error");
       exit(-1);
       }
       sort(lines, number_of_lines);
       print_strings(lines, number_of_lines);

return (0);
}
/* FUNCTION CALL */

int get_lines(char *lines[])
{
int n = 0;
char buffer[80];

puts("Enter one line at a time, enter a blank when done");

 while((n < MAXLINES) && (gets(buffer) != 0) && (buffer[0] != '\0'))
      {
      if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
      return -1;
      strcpy( lines[n++], buffer);
      }
      return n;
}

void sort(char *p[], int n)
{
int a, b;
char *x;

for (a = 1; a < n; a++)
  {
    for(b=0; b < n; b++)
    {
       if ( strcmp(p[b], p[b+1]) > 0)

      {
      x = p[b];
      p[b] = p[b+1];
      p[b+1] = x;
      }
    }
  }
}
void print_strings(char *p[], int n)
{

int count;
for (count = 0; count < n; count++)
     printf("%s\n ", p[count]);
}
ASKER CERTIFIED SOLUTION
Avatar of rbr
rbr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"10337606.c", line 32: warning(1164): argument of type "int **" is
          incompatible with parameter of type "char **"
       number_of_lines = get_lines(lines);
                                   ^

"10337606.c", line 39: warning(1164): argument of type "int **" is
          incompatible with parameter of type "char **"
     sort(lines, number_of_lines);
          ^

"10337606.c", line 40: warning(1164): argument of type "int **" is
          incompatible with parameter of type "char **"
     print_strings(lines, number_of_lines);
                   ^
Avatar of pyramid

ASKER

thanks