Making a String Array in C

Discussion in 'Mac Programming' started by aarplane, Aug 17, 2008.

  1. aarplane macrumors newbie

    Joined:
    Dec 8, 2005
    Location:
    Canada
    #1
    Hey guys,

    I'd like to make an array of strings in C, but I'm unsure of how to do so.

    Entries in this array will be output-ed to a field on a form.

    Thanks in advance!
     
  2. laprej macrumors regular

    Joined:
    Oct 14, 2005
    Location:
    Troy, NY
    #2
    There are a number of ways to do this. One way, if you just want to output various strings is this:

    Code:
    const char *out[] = {"string1", "string2", "string3"};
    printf("%s\n", out[1]);
    
    Another, if you're asking for strings, is this:
    Code:
    char input[10][200];
    scanf("%s", &input[3]);
    
     
  3. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #3
    Careful! input[3] is already a char*, so the extra ampersand is spurious. Also, this code is asking for a buffer overrun. If you must use scanf, at least tell it you don't want more than 199 characters (leaving room for the terminating null character):

    Code:
    scanf("%199s", input[3]);
    
     

Share This Page