• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

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..
0
pyramid
Asked:
pyramid
1 Solution
 
jkrCommented:
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...
0
 
makerpCommented:
if you attempt to printf a char * that aint been malloced you get that.
0
 
kishore_joshiCommented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
pyramidAuthor Commented:
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]);
}
0
 
rbrCommented:
for(b=0; b < n; b++)

must be
 for(b=0; b < (n-1); b++)
0
 
ozoCommented:
"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);
                   ^
0
 
pyramidAuthor Commented:
thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now