Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

null return comes up on screen

Posted on 2000-05-02
7
Medium Priority
?
229 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
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.

 
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 200 total points
ID: 2773217
for(b=0; b < n; b++)

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

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

916 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