Thread: how do you declare a single dimentional array over several lines

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    how do you declare a single dimentional array over several lines

    obviously you can initialize a single array like this
    Code:
    int myarray[5] = { 1, 2, 3, 4, 5 };
    but if you had a big array that needed to be initialized at declaration time how do you span multiple likes rather than one long string

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Everywhere you have a white space, you can have a newline.
    Code:
    int myarray[5] = {
      1, 2,
      3, 4,
      5
    };
    You can do the same with strings also.
    Code:
    char text[] = "The quick brown fox "
                  "jumps, over the lazy dog!";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    thanks they are about the only combinations i didn't try

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Are you trying to read the cipher data into an array? If so, instead of hard-coding it into your program text, you can read it directly from the file like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define INPUTFILE "cipher.txt"
     
    int get_text(char *text) {
        FILE *file = fopen(INPUTFILE, "r");
        if (!file) {
            perror(INPUTFILE);
            exit(EXIT_FAILURE);
        }
     
        int len = 0;
        // The %*c reads and ignores the comma after the integer.
        for (int ch; fscanf( file, "%d%*c", &ch ) == 1; )
            text[len++] = ch;
        fclose(file);
     
        return len;
    }
     
    int main() {
        char text[1500]; // Input has 1455 chars
        int len = get_text(text);
        printf("%d\n", len); // should print 1455
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    thanks you read my mind i was getting to the point of cut and paste and having it as a fixed array

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 dimentional array
    By gameover6005 in forum C Programming
    Replies: 2
    Last Post: 04-16-2013, 09:07 PM
  2. 3 dimentional array in C
    By jahanpanah in forum C Programming
    Replies: 3
    Last Post: 10-24-2010, 11:16 AM
  3. 2 dimentional array
    By theone1 in forum C++ Programming
    Replies: 10
    Last Post: 11-11-2007, 01:30 PM
  4. Two dimentional array
    By h3ro in forum C++ Programming
    Replies: 12
    Last Post: 04-17-2007, 01:01 AM
  5. Multi line quotes used on single lines
    By CBUK in forum C Programming
    Replies: 7
    Last Post: 02-03-2005, 08:11 AM

Tags for this Thread