0

I am new to C Programming ,I don't know what can I change in this code if I compile this code it displays only last name only for n times .. Why it won't display other names please help experts .. Thank you!

#include<stdio.h>
#include<string.h>
#include<malloc.h>
int main()
{
    int a;
    char n[50];
    printf("enter the number of students:\n");
    scanf("%d",&a);
    printf("enter the names of the students\n");
    int i;
    for(i=0;i<a;i++)
    {
        scanf("%s",n);
    }

    for(i=0;i<a;i++)
    {       

        printf("%s\n",n);

    }

return 0;

}
KNU
  • 2,560
  • 5
  • 26
  • 39
R.A
  • 59
  • 1
  • 1
  • 8

5 Answers5

3

char n[50] is an array of characters, which can store just one string of up to size 50. Here, you are overwriting the same string again and again with your scanf.

Arjun Sreedharan
  • 11,003
  • 2
  • 26
  • 34
1

Whenever you read a new name, you overwrite the last one. To avoid this, you must declare an array to store them. However, as your number of students is received from user input, you'll have to allocate it dynamically, e.g.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int a;
    int i;
    char **n;
    printf("enter the number of students:\n");
    scanf("%d",&a);

    n = malloc(sizeof(char*) * a);

    printf("enter the names of the students\n");

    for(i=0;i<a;i++)
    {
        n[i] = malloc(sizeof(char) * 50);
        scanf("%s",n[i]);
    }

    for(i=0;i<a;i++)
    {       
        printf("%s\n",n[i]);
    }

    for(i = 0;i < a;i++) {
        free(n[i]);
    }

    free(n);

    return 0;
}

and please avoid using malloc.h. Use stdlib.h instead.

Mauren
  • 1,955
  • 2
  • 18
  • 28
  • 'sizeof(char*)' ??? Is that valid? `error: invalid conversion from ‘void*’ to ‘char**’ [-fpermissive] n = malloc(sizeof(char*) * a);` – KNU Mar 13 '14 at 14:01
  • 1
    k , I get this error coz I had C++ as language on ideone. This should be the accepted answer. – KNU Mar 13 '14 at 14:10
0

You can store names in an array of pointers to char.

int a;
printf("enter the number of students:\n");
scanf("%d",&a);

 char *n[a]; // VLA; A C99 feature 
 // Allocate memory for all pointers.
 for(int i=0;i<a;i++)
 {
     n[i] = malloc(50);
 } 
 printf("enter the names of the students\n");
 for(int i=0;i<a;i++)
 {
     scanf("%s",n[i]);
 }  

and then print it as

for(i=0;i<a;i++)
{       

Note: Do not use scanf or gets to read string. It is better to use fgets function.

fgets(n[i], 50, stdin);
haccks
  • 104,019
  • 25
  • 176
  • 264
0

It only print the last one as each one will overwrite content of n of the previous one. You can print it out right after reading.

Change

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

for(i=0;i<a;i++)
{       

    printf("%s\n",n);

}

to

for(i=0;i<a;i++)
{
    scanf("%s",n);
    printf("%s\n",n);
}
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
0

An alternative but less efficient answer to Mauren's. I am posting so as to help you understand other possibilities.

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

#define MAXLEN   50   /*..max #character each name can have*/
#define MAXLINES 5000 /*..max #lines allowed*/

int main()
{
int a;
int i;
char *n[MAXLINES]; //pointer to text lines
printf("enter the number of students:\n");
scanf("%d",&a);

printf("enter the names of the students\n");

for(i=0;i<a;i++){
    n[i] = malloc(sizeof(char) * MAXLEN);
    //same as : *(n+i) = malloc(sizeof(char) * 50);
    scanf("%s",n[i]);
}

for(i=0;i<a;i++){       
    printf("%s\n",n[i]);
}

for(i = 0;i < a;i++){
    free(n[i]);
}

free(n);
system("pause");
return 0;
}

Suggested reading : Pointer Arrays; Pointers to Pointers

KNU
  • 2,560
  • 5
  • 26
  • 39