0

Here is the C code.

#include <stdio.h>
#include <conio.h>

main()
{
    int i,n;
    char a[100];

    printf("Enter number of teams");
    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
        printf("\nenter team %d",i);
        scanf("%s",a);
    }

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

My program is simple, but its crashing. i just want to load in a few names into array and then print them back. I am able to do that but at last line, its crashing.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
ThisSiteSucks
  • 145
  • 2
  • 4
  • 12
  • 3
    `printf("%s", a[i]);` is wrong because type of `a[i]` is `char`, you must enable compiler warnings. – Iharob Al Asimi Feb 08 '15 at 17:26
  • 2
    array index start from 0 and goes till n-1. and what are you expecting with `scanf("%s",a);` in a loop? – SMA Feb 08 '15 at 17:26
  • 2
    Also, arrays are indexed from 0, not 1. – jpw Feb 08 '15 at 17:26
  • 3
    You have some serious misunderstandings of very basic stuff. You should read a [good book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to get that sorted out. – Baum mit Augen Feb 08 '15 at 17:28
  • 1
    I'm voting to close this question as off-topic because OP doesn't have basic understanding of strings/arrays in C – P0W Feb 08 '15 at 17:30
  • 1
    @POW which is why the OP asked this question. – Iharob Al Asimi Feb 08 '15 at 17:36
  • @downvoters(whoever they maybe, I am not addressing any particular person, I am just saying in general) .My account has been banned from asking questions and the stackoverflow has asked me to review my questions. I don't see how this question is wrong by any means. So if there is any improvement, please suggest so, or else please consider upvoting this question, so I get back my ability to ask questions on this site. I like stackoverflow and would like to be part of this community, so I want your help either in terms of suggestions for re-formatting the question or in form of upvotes. Thanks – ThisSiteSucks Mar 16 '16 at 08:15

2 Answers2

3

A char[] is equivalent to a string in C when it is terminated with a NUL character (\0). You need to create a char[][], or an array of char[]s.

So, an array of strings in C can be defined as:

char arr[5][100];
//The 5 is the number of strings, and 100 is the length of a single string.

Also, in C, array indexing starts from 0, not 1.

Code:

#include<stdio.h>
int main()
{
    int i,n;
    printf("Enter number of teams");
    scanf("%d",&n);
    char a[n][100];
    for(i=0;i<n;i++)
    {
        printf("\nenter team %d",i);
        scanf("%s",a[i]);
    }
    for(i=0;i<n;i++)
    {
        printf("%s\n",a[i]);
    }
}
shauryachats
  • 9,975
  • 4
  • 35
  • 48
1

You have a few errors in your code

  1. main() must return int

  2. a should be an array of strings, and it's an array of char, you can do it with an array of char arrays, declare it as

    char a[100][100];
    
  3. Arrays in c are indexed from 0 to n - 1 and not from 1 to n.

  4. You must check if the first scanf() worked, furthermore, you must check that n is not too big so this part does that

    if (scanf("%d", &n) != 1)
        return 1;
    if (n > 100)
        n = 100;
    
  5. You should prevent a buffer overflow, for that you can use the length specifier for scanf(), if your array can store 100 char's then you need to use

    scanf("%99s", a[i]);
    

    because you also need the terminating '\0'.

  6. Your printf() statement is using the wrong specifier, because you are passing a char and the "%s" is specting a c string.

Try this

#include <stdio.h>

int
main()
{
    int i, n;
    char a[100][100];

    printf("Enter number of teams");
    if (scanf("%d", &n) != 1)
        return 1;
    if (n > 100)
        n = 100;

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

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

    return 0;
}

also, even though you are not explicitly manipulating pointers, you can't avoid that completely, because for example printf() takes a pointer parameter for the "%s" specifier, it's just that in this case you don't explicitly pass a pointer, but the i-th array of the array of char arrays will automatically decay to a pointer in

printf("%s", a[i]);
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    Minor: Consider stating the most important issues first (#2 & #3). Also use differ sizes in array rather than 100,100, maybe `char a[20][50];` or `#define M,N`. It helps in understanding code like `if (n > 20) .... scanf("%49s", a[i]);` – chux - Reinstate Monica Feb 08 '15 at 19:58