Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6793479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:06:20+00:00 2026-05-26T18:06:20+00:00

I’m working on an exercise in K&R (ex. 5–9) and I was trying to

  • 0

I’m working on an exercise in K&R (ex. 5–9) and I was trying to convert the original program’s 2D array of

static char daytab[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

into using pointers to an array of 13 ints like

static char (*daytab)[13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

But compiler prints warning: excess elements in scalar initializer.

Googling did not help and even K&R writes when passing the array to a function,

myFunction(int daytab[2][13]) {...}

is the same as

myFunction(int (*daytab)[13]) {...}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:06:20+00:00Added an answer on May 26, 2026 at 6:06 pm

    The two are only partly equivalent. The difference being that:

    static char daytab[2][13] = {
        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    

    declares a two-dimensional array, which includes setting aside space for the array and ensuring that daytab references that memory. However:

    static char (*daytab)[13] = {
        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    

    …only declares a pointer. So you’re trying to initialize a pointer with an array initializer, which doesn’t work as expected. There is no array; there’s no memory set aside for an array. What happens instead is that the first number in your initializer is assigned to the pointer daytab, and the compiler generates a warning to let you know you’ve specified a lot of additional values that are just discarded. Since the first number in your initializer is 0, you’re just setting daytab to NULL in a rather verbose way.

    So if you want to do this sort of initialization, use the first version — it decays to the same pointer type that you explicitly declare in the second version, so you can use it the same way. The second version, with the array pointer, is needed when you wish to dynamically allocate the array or get a reference to another array that already exists.

    So you can do this:

    static char arr[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
    static char (*ptr)[3] = NULL;
    
    ptr = arr;
    

    …and then use ptr and arr interchangeably. Or this:

    static char (*ptr)[3] = NULL;
    
    ptr = malloc(2 * sizeof(*ptr));
    

    …to get a dynamically allocated 2-dimensional array (not an array of pointers to 1D arrays, but a real 2D array). Of course, it’s not initialized in that case.

    The “equivalence” of the two variations just means that the 2D array, when it decays to a pointer to its first element, decays to the type of pointer declared in the second variation. Once the pointer version is actually pointed at an array, the two are equivalent. But the 2D array version sets up memory for the array, where the pointer declaration doesn’t… and the pointer can be assigned a new value (pointed at a different array) where the 2D array variable cannot.

    In C99 you can do this, though (if not static at least):

    char (*daytab)[13] = (char [][13]){
        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm going through K & R, and am having difficulty with incrementing pointers. Exercise
I'm working on an exercise from Accelerated C++: Write a program to count how
I'm working through the exercises in the K&R book. Currently I'm stuck at exercise
Trying to learn C++ and working through a simple exercise on arrays. Basically, I've
I am trying to do an exercise where I translate a grammar into Chomsky
I am working on Exercise 11.11 on page 498 of C++ How to program
I have got this exercise working perfectly: import acm.program.*; import acm.graphics.*; import java.awt.event.*; public
While working through this exercise I ran into a problem. from sys import argv
So I've been working the last exercise of Learn Python the Hard Way and
I'm currently working on a self-teaching exercise in programming in C# to make an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.