-1

when input is given five ,instead of asking for 5 string it takes only four ,Why is it so ? why by default *a+0 = '\n' is saved ? I have also tried scanf("%d %c", &n &ch ) at line 9,but the problem was same.

    #include <stdio.h>
    #include <string.h>

int main()
    {
        int n;
        char ch;
        printf("no of elements\n");
        scanf("%d ", &n ); //line 9
        //asking for number of pointer in array 
        char *a[n];
        puts("string");
        for (int i = 0; i < n; ++i){
            gets(a+i);
        }
        puts("-----");
        for (int j = 0; j < n; ++j){
            puts(a+j);
        }
        puts("-----");
        puts(a);
        puts("-----");
        puts(a+2);

        return 0;
    }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

1

You probably want this:

#include <stdio.h>
#include <string.h>

int main()
{
  int n;
  printf("no of elements\n");
  scanf("%d", &n);  // No space after the %d !

  char dummy[2];            // these 2 lines are needed for absorbing the leftover
  fgets(dummy, 2, stdin);  // \n from scanf (mixing fgets and scanf is a bad idea)

  char *a[n];       // here we'll have an array of n pointers to char. For the moment
                    // the array is not initialized and the pointers point nowhere

  puts("string");

  for (int i = 0; i < n; ++i) {
    a[i] = malloc(100);         // allocate memory for a string of a maximum length of 99.
                                // + one char for the NUL terminator
                                // Allocating a fixed size is awkward, but it will
                                // do here for demonstration purposes

    fgets(a[i], 100, stdin);    // limit the length of string to 100 in order
                                // to avoid buffer overflow
                                // it's basically the same as gets(buffer)
  }

  puts("-----");
  for (int j = 0; j < n; ++j) {
    puts(a[j]);
  }

  puts("-----");
  puts(a[0]);
  puts("-----");
  puts(a[2]);

  return 0;
}

Input and output:

no of elements
3
string
11
22
33
-----
11

22

33

-----
11

-----
33

Error checking is avoided for brevity.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

The declared variable in this declaration

char ch;

is not used in the program and should be removed.

And neither declaration from the header <string.h> is used in your program. So the header could be removed.

You declared a variable length array of pointers to the type char

char *a[n];

However the elements of the array were not initialized and have indeterminate values. As result the program has undefined behavior due to this statement in the for loop

gets(a+i);

You have to allocate memory for each string you are going to enter.

Also take into account that the function gets is unsafe and is not supported any more by the C Standard. Instead use the function fgets. Moreover the argument in the function call must be *( a + i ) instead of a + i because the type of the last expression is char ** instead of the required type char *.

So a valid code can look for example the following way

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

int main( void )
{
    size_t n;
    const size_t SIZE = 20;

    printf( "no of elements: " );

    if ( scanf( "%zu%*c", &n ) != 1 || n == 0 ) n = 1;

    char * a[n];

    for ( size_t i = 0; i < n; i++ )
    {
        *( a + i ) = malloc( SIZE );    
    }

    puts("string");

    for ( size_t i = 0; i < n; ++i )
    {
        fgets( *( a + i ), SIZE, stdin );
    }

    puts("-----");

    for ( size_t i = 0; i < n; ++i )
    {
        printf( "%s", *( a + i ) );
    }

    puts( "-----" );
    printf( "%s", *a );

    puts( "-----" );
    printf( "%s", *( a + 2 ) );

    for ( size_t i = 0; i < n; i++ )
    {
        free( *( a + i ) );
    }

    return 0;
}

Its output might look like

no of elements: 5
string
A
B
C
D
E
-----

A
B
C
D
E
-----
A
-----
C

Pay attention to this statement

    if ( scanf( "%zu%*c", &n ) != 1 || n == 0 ) n = 1;

After reading the variable n using format specifier &zu you need to remove from the input buffer the new line character that corresponds to the pressed key Enter. Otherwise the next call of fgets will read an empty string.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335