Home > Software design >  Why can't I initialize a "const char chars [ 5 ] [ 10 ]" array with "*"-s (
Why can't I initialize a "const char chars [ 5 ] [ 10 ]" array with "*"-s (

Time:01-18

Why does this:

const char chars [ 2 ] [ 10 ] = {
  { "*", "*", "*", "*", "*", "*", "*", "*", "*", "*" },
  { "*", "*", "*", "*", "*", "*", "*", "*", "*", "*" }
 };
 for ( int i = 0; i < 2;   i ) {
  for ( int j = 0; j < 10;   j ) {
   printf ( chars [ i ] [ j ] );
   if ( j != 9 )
    printf ( " " );

give this:

c.c: In function ‘main’:
c.c:27:10: error: excess elements in char array initializer
   27 |   { "*", "*", "*", "*", "*", "*", "*", "*", "*", "*" },
      |          ^~~
c.c:27:10: note: (near initialization for ‘chars[0]’)

and the same with "0", "1" instead of " * ", but const char* [ 2 ] [ 10 ] works ?

CodePudding user response:

char chars[2][10] is an array of arrays of chars. Each char in this 2D array could be initialized with a single char, OR each subarray of chars could be initialized with a string. Your're trying to initialize each char with a string which won't work. Try:

const char chars[2][10] = {
  { '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' },
  { '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' }
};

or

const char chars[2][10] = { "**********", "**********" };

You need to be careful as these subarrays of char are not valid strings -- they are missing a NUL terminator. So you can't treat them as strings.

Note also, to print a char, you need printf("%c", chars[i][j]); or putchar(chars[i][j]);

CodePudding user response:

There is more than one issue in your code. At first, in C double quotation means string but you want to initialize with char in which single quotation should be used. Other than that, some parentheses are also missing in your code.

And when you want to print some variables in C using printf, inside the printf you have to specify which type of data you want to print (in this case char which specifier is %c).

I assume you wanted to do this:

#include <stdio.h>

const char chars [ 2 ] [ 10 ] = {
  { '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' },
  { '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' }
 };

int main () {
    for ( int i = 0; i < 2;   i ) {
        for ( int j = 0; j < 10;   j ) {
            printf ("%c", chars [ i ] [ j ] );
            if ( j != 9 )
                printf ( " " );
        }
        printf("\n");
    }
}

CodePudding user response:

There are two errors:

  1. When initializing a 2D array, either 'c' or "string" should be used.
  2. Use %c format to print the char value with printf().
#include <stdio.h>

int main()
{
    const char chars [ 2 ] [ 10 ] = { { "**********" }, { "**********" } };
    
    for ( int i = 0; i < 2;   i ) 
    {
        for ( int j = 0; j < 10;   j ) 
        {
            printf ("%c", chars [ i ] [ j ] );
            
            if ( j != 9 )
                printf (" ");
            else 
                printf("\n");
        }
     }
    return 0;
}

CodePudding user response:

You must use single quotes not double quotes. Double quotes imply a null-terminated string, whereas single quotes are single chars. So, a single character inside double quotes is actually 2 bytes since it automatically gets a null terminator (binary zero) added to the end.

  •  Tags:  
  • Related