This simple code is producing weird output.

I write a line and press enter, it echo's the line...well, supposed to...Because, sometimes, it is not showing.

In code blocks, i can assume that when the program is returning zero the program is terminating correctly.

I tried to figure out, when it shows something different then zero..i found that no matter what the program termination status is(zero or some other garbage number), the output is completely random...sometimes, it is showing, and sometimes it is not.

Can anyone tell me why?

#include<stdio.h>

int main()
{
    char *array;

    gets(array);

    while(*array)
        printf("%c", *array++);
        
    return 0;
}

Later on, i tried to execute the problem in Visual C++...but, the program shows crashes after entering the line...so i concluded that the problem is with this :

gets(array);

But, why sometimes, the code is running in codeblocks if the code is wrong?(Codeblocks is using GCC)

I wanted to take a line input of infinite length.Can this be a way to implement the plan?

Recommended Answers

All 3 Replies

>Can anyone tell me why?
Your pointer is uninitialized. It's not a pointer to infinite memory, it's an invalid pointer. Expect crashes and unpredictable results, because that's what happens with undefined behavior.

>I wanted to take a line input of infinite length.
It's not that easy. You need to read into a single character or a block of characters, resize your buffer, and then copy into it until input is complete. For example:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define AGETS_PARTIAL 0x8F5

/*
  Read characters into a result string until '\n', EOF, or error
    * NULL is returned if no characters were stored,
      otherwise the current string is returned
    * The '\n' character is not retained
  The result string is dynamically allocated and must be freed
  errno is set to AGETS_PARTIAL if a partial string is returned
*/
char *agets ( FILE *in )
{
  char *buf = NULL;    /* Destination buffer */
  size_t capacity = 0; /* Current buffer capacity */
  size_t size = 0;     /* Current number of characters in the buffer */

  for ( ; ; ) {
    int ch;

    if ( size == capacity ) {
#define CHUNK_SIZE 8
      /* Make space for the null character, but don't include it in capacity */
      char *save = realloc ( buf, ( capacity += CHUNK_SIZE ) + 1 );
#undef CHUNK_SIZE

      if ( save == NULL ) {
        if ( buf != NULL )
          errno = AGETS_PARTIAL;

        break;
      }

      buf = save;
    }

    /* Done on EOF or a newline, don't include in the buffer */
    if ( ( ch = getc ( in ) ) == '\n' || ch == EOF )
      break;

    buf[size++] = (char)ch;
  }

  /* Don't forget to terminate the string */
  if ( buf != NULL )
    buf[size] = '\0';

  return buf;
}
commented: Nice code :) +6

Thanks for the help...

Now I am reading the pointer tutorial from your site....I was really looking for something like this!!

Thanks again :D :D

And try not to use gets() . It's an erratic function. To cut a long post short, look here

commented: Exactly. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.