-3

I try to type simple 2D array of string take strings from user and print it

void in(char* n[3][2]);
void show_name ()
{
    char* n[3][2];
    in(n);
    unsigned int i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<2;j++)
            printf("%s ", &n[i][j]);
        printf("\n");
    }
}

int main(void)
{
    show_name();
    return 0;
}
void in(char* n[3][2])
{

   int i,j;
    for(i=0;i<3;i++)
        for(j=0;j<2;j++)
            scanf("%s",&n[i][j]);

    printf("\n");
}

it works correctly but i have warning say:

warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char**' [-Wformat]|

I searched for reason i found that the problem in %s doesn't need for address when i remove & operator there`s no warning but code doesn't run correctly

Mohamed Slama
  • 189
  • 1
  • 3
  • 17
  • 5
    I think the warning message should be pretty obvious. – Some programmer dude May 19 '15 at 18:09
  • no need of char *n[3][3] in show_name(), use void in(char **n), and call it as in(n); – Subinoy May 19 '15 at 18:09
  • 2
    There is a lot wrong here: * you do not check the size of the input, which makes buffer over flows possible * you most likely want to scan in 3 strings, but call scanf 6 times. – mfuchs May 19 '15 at 18:10
  • As for it not working properly, it'b probably because you an an array of arrays of pointer, and you never initialize the pointer so writing to them with e.g. `scanf` will lead to *undefined behavior*. And that's not the *actual* problem with that `scanf` call as it is now. – Some programmer dude May 19 '15 at 18:10
  • yeah you need to run the loop for three times only, as @mat69 told – Subinoy May 19 '15 at 18:11
  • possible duplicate of [C - warning: format ‘%s’ expects type ‘char \*’, but argument 2 has type ‘char (\*)\[20\]’](http://stackoverflow.com/questions/16570716/c-warning-format-s-expects-type-char-but-argument-2-has-type-char) – Politank-Z May 19 '15 at 18:13
  • @mat69 i want to input 6 times but doesn't know how to do it – Mohamed Slama May 19 '15 at 18:37
  • @Politank-Z it`s not the same – Mohamed Slama May 19 '15 at 18:42
  • @MohamedSlama you specified that you want 3 strings which have a size of 2 each. C strings are null-terminated, that means they end with '\0', so "foo" would take 4 bytes, because it consists of the 4 characters `{'f', 'o', 'o', '\0'}`. That means every of your 3 strings could contain just 1 character + the terminating null-byte. In @WeatherVane 's answer 3 strings can be read in with a maximum size of 200 bytes each. Replace the 3s with 6 and you can read in 6 strings. – mfuchs May 19 '15 at 18:42
  • @Subinoy how to do that implement it please i try to do that but i can`t – Mohamed Slama May 19 '15 at 18:45
  • @MohamedSlama I have shown you how to do it my answer, now edited to allow you to use any number of strings with one simple change. – Weather Vane May 19 '15 at 18:54
  • Does this answer your question? [scanf and char variable problems](https://stackoverflow.com/questions/11426815/scanf-and-char-variable-problems) – outis Apr 15 '22 at 04:23

1 Answers1

1

I have simplified and corrected your program, commenting where I changed it.

EDIT how to make it easy to change the number of strings, now 6 as above comments

#include <stdio.h>

#define STRINGS 6

void in(char n[][200]);         // no need for * and array

void show_name ()
{
    char n[STRINGS][200] = {};  // allow some string space and init for security
    int i;
    in(n);
    for(i=0; i<STRINGS; i++)    // only need to iterate one dimension
        printf("%s\n", n[i]);
}

int main(void)
{
    show_name();
    return 0;
}

void in(char n[][200])          // needs length of each row, but not number of rows
{
    int i;
    for(i=0; i<STRINGS; i++)    // only need to iterate one dimension
        scanf("%199s", n[i]);   // restrict the input length
    printf("\n");
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • what`s the difference now between it and 1D array ?? – Mohamed Slama May 19 '15 at 18:32
  • edit your code and remove '&' operator because it still give the same warring – Mohamed Slama May 19 '15 at 18:39
  • It's a good point. My compiler doesn't give a warning there, I understand that to be handled properly even if it is strictly incorrect. – Weather Vane May 19 '15 at 18:43
  • It's a 2D array of `char`, but each "string" is a 1D `char` array. When you iterate as strings, that's effectively 1D too. `n[i]` gets converted to `char*` by the compiler. – Weather Vane May 19 '15 at 18:45
  • i really misunderstand 2d array now in c :) when i implement it in c# , Java and python it look like the real one in mathematics needs 2 loops one for the rows and one for the column – Mohamed Slama May 19 '15 at 18:57
  • Well so you would here. A 2D array is declared and 2D it is. But each element is a `char` not a string. There is no C string type, it's an array of `char` terminated by a `0`, so that takes one of the dimensions. – Weather Vane May 19 '15 at 19:01