0

I'm trying to scan in a grid of letters. cases is the number of letter grids and r, c are for number of rows and columns of each grid. I figured I could create an array of structs with 2D arrays inside. I've been working on this for a few hours now and it's still giving me problems:

  • Warnings:

    • Warning C4477 (Line 12) - ‘scanf_s’ : format string ‘%s’ requires an argument of type ‘char*’, but variadic argument 1 has type ‘int’
    • Warning C4473 (Line 12) - ‘scanf_s’ : not enough arguments passed for format string
  • Errors:

The code:

scanf_s("%d", &cases);
struct grid { 
    char **grid; 
};
struct grid *grids = (struct grid*)malloc(cases * sizeof(struct grid));

for (i = 0; i < cases; i++) {
    scanf_s("%d %d", &r, &c);
    grids[i].grid = (char**)malloc(sizeof(char*) * r);
    for (k = 0; k < r; k++) {
        grids[i].grid[k] = (char*)malloc(sizeof(char) * (c+1));
        scanf_s("%s", grids[i].grid[k], (c+1));
    }           
}
CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • `scanf_s("%s", grids[i].grid[k]);` → `scanf_s("%s", grids[i].grid[k], c + 1);`. [`scanf_s` requires a third argument for `%s`, `%c` etc denoting the maximum size](https://msdn.microsoft.com/en-us/library/w40768et.aspx). Also, it is recommended to [avoid casting `malloc`'s return value](http://stackoverflow.com/q/605845/3049655) – Spikatrix Jan 29 '17 at 08:25
  • The first compiler mistake could be caused by giving your `struct grid` a field named `grid` ; i've never tried, but i doubt it's allowed. – m.raynal Jan 29 '17 at 08:27
  • I thought malloc's return value is void and that's why you always have to cast it? – Christopher Jan 29 '17 at 08:34
  • 2
    @m.raynal: There is no problem with that. – David Ranieri Jan 29 '17 at 08:35
  • No, the call to malloc automatically promotes the pointer to the right type. @keine Lust, thanks, i'll know, but it definitly seems to me being a bad habit a source of confusion. – m.raynal Jan 29 '17 at 08:35
  • Why are you not checking result of `scanf_s()`? – RoadRunner Jan 29 '17 at 08:37
  • How do you mean checking result? My code will not compile because of the lines above. – Christopher Jan 29 '17 at 08:41
  • 3
    The exact *lines* where those linked errors happen should be noted in the posted source code, and the full text of the error messages should *always* be included *verbatim* in questions asking about error messages. – WhozCraig Jan 29 '17 at 08:51
  • What is terminal output? – Christopher Jan 29 '17 at 08:56
  • 1
    Post whatever error message you are getting, so that we can get a better understanding of the problem. It compiled and ran without any errors in my Ubuntu system (of course I replaced `scanf_s()` with `scanf()`). – Jithin Pavithran Jan 29 '17 at 09:03
  • 2
    "*constructor syntax missing formal parameters*" this heavily sounds like C++. Are you sure you are using a C compiler? – alk Jan 29 '17 at 11:08
  • 1
    So please, *what* are the lines 3, 9, and 12? :-/ – alk Jan 29 '17 at 11:09

1 Answers1

0

I had to tweak a little bit the code in the question, also to add some missing parts (that should have also been provided, btw), to get a compilable (using VStudio2010) piece.

main00.c:

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


struct grid { 
    char **grid; 
};


int main() {
    int cases, r, c, k, i;
    struct grid *grids;

    scanf_s("%d", &cases);
    grids = (struct grid*)malloc(cases * sizeof(struct grid));

    for (i = 0; i < cases; i++) {
        scanf_s("%d %d", &r, &c);
        grids[i].grid = (char**)malloc(sizeof(char*) * r);
        for (k = 0; k < r; k++) {
            grids[i].grid[k] = (char*)malloc(sizeof(char) * (c + 1));
            scanf_s("%s", grids[i].grid[k], (c + 1));
        }           
    }
    return 0;
}

Notes:

  • You were compiling the code as C++ (as @alk noticed), and this is what completely threw you off course. In order to fix that either:
    • Set your file extension to .c (and you can leave the VStudio defaults in term of compilation; that way it will compile each source file using the appropriate compiler based on its extension)
    • Explicitly compile the source file as C. You can set that from VStudio Project Properties -> C/C++ -> Advanced -> Compile As, and choosing Compile as C Code. For more info, visit [MS.Docs]: /Tc, /Tp, /TC, /TP (Specify Source File Type). Personally, I think that the former option is more straightforward
  • Move the variable declarations before any statements (I think this might have been a candidate for setting the C++ compiler). This was a restriction for older C standards (I doubt that it still stands), but (VStudio 2010) C compiler still enforces it (at least by default)
CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • Thank you CristiFati. That was the problem. When I switched it over to a .c file it did run but I got a whole new line of problems with it not showing any new errors and crashing. It would not even take any new changes so I just switched to Codeblocks. I don't think I have a deep enough knowledge to troubleshoot VS right now. – Christopher Jan 29 '17 at 19:15
  • Then would you please mark the answer as a solution, so it's clear that it solves the problem? – CristiFati Jul 27 '18 at 20:22