1

I made this to just seal in strcpy() to my brain, and I noticed that if I leave the brackets empty the compiler says it needs an initializer or needs a specified value to compile. However I can put any value into the brackets and it still compiles. Even with a zero... But if the strcpy() function adds a string terminator to whatever string is declared, why would I still need to put a placeholder in the brackets?

Code below...

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

main()
{
    char yearFirst[0];  <------ HOW DOES THIS still EXECUTE??
    char yearSecond[6];
    char month[5];

    /* when declaring strcpy function, place each copied string before.
     their desired print function.
    or else the print function will print thr same strcpy for each print 
    function proceeding it.

    ex.
      strcpy(yearFirst, "Sept., Oct., Nov., Dec., Jan.");
      strcpy(yearSecond, "Mar., Apr., May., Jun. Jul., Aug.");
      followed by:
        printf("These months have 1-31 days: %s\n\n", yearFirst)
        printf("These months have 1-30 days: %s\n\n", yearSecond);

    output will equal both statements saying
    "These months have 1-31: sept oct ...."
    "these months have 1-30: sept oct....."
    */ 

    strcpy(yearFirst, "Sept., Oct., Nov., Dec., Jan.");
    printf("These months have 1-31 days: %s\n\n", yearFirst);

    strcpy(yearSecond, "Mar., Apr., May., Jun. Jul., Aug.");
    printf("These months have 1-30 days: %s\n\n", yearSecond);

    strcpy(month, "Feb.");
    printf("%s has 1-28 days\n", month);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
JNeill
  • 11
  • 1
  • Note that you should be coding to at least C99 and preferably C11 and you should be using an explicit return type on `main()`. C99 dropped support for the missing return type. – Jonathan Leffler Sep 27 '16 at 06:55

1 Answers1

2

Only GCC allows zero-length arrays as a non-standard extension (and some compilers being compatible with GCC allow them too).

You can't store anything in such an array, of course, so passing yearFirst as an argument to strcpy() — whether the first or second argument — leads to undefined behaviour. Anything can happen. A crash is likely; it might even appear to work. Don't do it. Make sure you allocate enough space to store whatever you plan to copy into the array. It is a prime requirement of C programming; it is why some people prefer other languages where they don't have to pay attention to this sort of detail. But the C compiler isn't required to diagnose your dereliction of duty.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • For reference, im reading the absolute beginners guide to c. His examples use the return 0; also please excuse the formatting im using my phone to post and reply. So from what youre saying, i have to account for the number of characters inside the string. And the only reason my program runs is because the compiler just accepts the formatting as is due to a newer gcc encoder. I dont mind learning about dealing with memory coponents in C. I havent touched programming in years and started with c++. I didnt make it too far so decided to learn c and go from there. Thanks for the insight – JNeill Sep 27 '16 at 07:06
  • Maybe you shouldn't be using a book that is teaching what have been bad practices for all of the current millennium, not to mention some of the last. I don't know the book, but if the code you're copying is faulty like that, please do yourself a favor and get a book written within the last decade or so. – Jonathan Leffler Sep 27 '16 at 07:10
  • Copyright 2014 written by greg perry and dean miller. 3rd edition. – JNeill Sep 27 '16 at 07:12
  • Interesting that it is a recent edition. Check the C book list here to see whether it gets recommended or not. Often, third edition means there is some value in the book, but then there are Schildt's books as proof-positive that it is not a wholly reliable indicator. As I said, I don't know the book even by reputation. But if it writes `main()` instead of `int main(void)` (for preference; `int main()` is just about OK), it leaves room for improvement. OTOH, I like that it uses explicit `retuen 0;` in `main()`. That change in C99 was not for the better. – Jonathan Leffler Sep 27 '16 at 07:20
  • Ill take a look at the list. Im teaching myself so some of the nuances need explained to me. The code i provided was my own making amd not an example from the book. I was tedting my knowledge from what i read. And from what you have told me it would appear i implemented the array in the wrong way. – JNeill Sep 27 '16 at 07:28
  • Fair enough. I will observe that the [ACCU](http://accu.org) no longer has a review for the book, but it did in times past (I found evidence of reviews for the first and second editions; neither was marked 'recommended' or 'not recommended'). See [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) for more info. – Jonathan Leffler Sep 27 '16 at 07:39