Solved

null return comes up on screen

Posted on 2000-05-02
7
220 Views
Last Modified: 2012-05-04
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
Comment
Question by:pyramid
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2770781
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
 
LVL 10

Expert Comment

by:makerp
ID: 2772620
if you attempt to printf a char * that aint been malloced you get that.
0
 

Expert Comment

by:kishore_joshi
ID: 2772977
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:pyramid
ID: 2773110
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
 
LVL 10

Accepted Solution

by:
rbr earned 50 total points
ID: 2773217
for(b=0; b < n; b++)

must be
 for(b=0; b < (n-1); b++)
0
 
LVL 84

Expert Comment

by:ozo
ID: 2775039
"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
 
LVL 1

Author Comment

by:pyramid
ID: 2775684
thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now