Solved

null return comes up on screen

Posted on 2000-05-02
7
224 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

821 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