0

So I have to scan text from a file into an array, but I am only allowed to use scanf() to achieve this. In the file, there are 22 lines and the longest line is 455 characters long. When I compile, I get various warnings. I have been trying to find a solution but nothings seems to help my list of warnings and errors.

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

#define NUM_ROWS 22
#define LENGTH_OF_LONGEST_LINE 455
void textInit(char array[]);

int main(void) {
   char patty [NUM_ROWS][LENGTH_OF_LONGEST_LINE];

   textInit(patty[NUM_ROWS][LENGTH_OF_LONGEST_LINE]);


   return 0;
}


void textInit(char array[]) {

   int x = 0;

   for (x = 0; x < 22; ++x) {
      scanf("%455s", array[x]);
   }
   return;
}

The errors I recieve are:

StPat.c: In function âmainâ:
StPat.c:14:4: warning: passing argument 1 of âtextInitâ makes pointer from 
integer without a cast [enabled by default]
    textInit(patty[NUM_ROWS][LENGTH_OF_LONGEST_LINE]);
    ^
StPat.c:9:6: note: expected âchar *â but argument is of type âcharâ
     void textInit(char array[]);
     ^
StPat.c: In function âtextInitâ:
StPat.c:26:7: warning: format â%sâ expects argument of type âchar *â, but
argument 2 has type âintâ [-Wformat=]
       scanf("%455s", array[x]);
5
  • 2
    like this
    – BLUEPIXY
    Commented Mar 18, 2017 at 6:28
  • You should post it here for a single line read example. Commented Mar 18, 2017 at 6:35
  • Don't vandalize your questions.
    – anon
    Commented Mar 18, 2017 at 15:31
  • I'm voting to close this question as off-topic because the user removed the actual code Commented Mar 18, 2017 at 15:35
  • @user3629249 Double-check that, please.
    – anon
    Commented Mar 18, 2017 at 15:39

2 Answers 2

1

LENGTH_OF_LONGEST_LINE should be 456 to allow space for the \0 terminator.

The body of the function textInit() expects a 2d array, but the function signature only indicates an array of chars. You should also pass the dimensions of the array to the function, rather than rely on global constants.

When you call the textInit() function, you are attempting to pass a char instead of a 2d array (really, a 2d array name in a function call decays to a pointer to an array). The attempted access is out of bounds.

Finally, in the textInit() function, avoid using magic numbers, as you have in the loop in this function. And you should use size_t for array indices, as it is an unsigned integer type guaranteed to hold any array index.

Here is a modified version of your code:

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

#define NUM_ROWS 22
#define LENGTH_OF_LONGEST_LINE 456

void textInit(char array[][LENGTH_OF_LONGEST_LINE], size_t rows, size_t cols);

int main(void)
{
   char patty [NUM_ROWS][LENGTH_OF_LONGEST_LINE];

   textInit(patty, NUM_ROWS, LENGTH_OF_LONGEST_LINE);

   return 0;
}


void textInit(char array[][LENGTH_OF_LONGEST_LINE], size_t rows, size_t cols)
{
    size_t x = 0;

   for (x = 0; x < rows; ++x) {
      scanf("%455s", array[x]);
   }

   return;
}
1
  • Or, for (x = 0; x < rows;) if (scanf ("%455[^\n]%*c", array[x]) == 1) x++; Or, better yet use fgets and test for and strip the trailing '\n'... Commented Mar 18, 2017 at 6:12
1

In addition to the nul-byte catch by Mr. Bowling, you can also make use of fgets to take the input. If you would like to init the entire array, you can pass an array of pointers to type char LENGTH_OF_LONGEST_LINE and initialize the entire array at once. For example:

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

enum { MAXROWS = 22, MAXLINE = 456 };

size_t textinit (char (*a)[MAXLINE], size_t rows, size_t cols);

int main(void) {

    size_t i, n;
    char patty [MAXROWS][MAXLINE] = {""};

    n = textinit (patty, MAXROWS, MAXLINE);

    for (i = 0; i < n; i++)
        printf (" string[%2zu] : %s\n", i, patty[i]);

    return 0;
}


size_t textinit (char (*a)[MAXLINE], size_t rows, size_t cols)
{
    size_t n = 0;

    while (n < rows && fgets (a[n], cols, stdin)) 
    {
        size_t len = strlen (a[n]); /* get lenght */
        if (a[n][len - 1] == '\n')  /* check for '\n' */
            a[n][--len] = 0;        /* overwrite with nul-byte */
        n++;
    }

    return n;   /* return number of strings read */
}

Note, that fgets reads up to and including the '\n', so you remove the newline by getting the length of each string read, then testing for, and overwriting the '\n' with a nul-terminating character 0 (or the equivalent '\0').

Also note, if you are on windows, replace size_t with unsigned and %2zu in the printf with simply %2u.

2
  • "if you are on windows, replace size_t with unsigned and %2zu in the printf with simply %2u" - Why? Doesn't windows support size_t?
    – Spikatrix
    Commented Mar 18, 2017 at 8:18
  • It depends on the compiler. Earlier versions of windows compilers do not implement the 'z' prefix for size_t designation (they have size_t of course, but they use a plain %u to print the type) I've seen this with folks trying to compile with Codeblocks, etc.. Commented Mar 18, 2017 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.