I've a list of questions regarding pointers and strings. I was doing K&R section 5.8. In the section a function month_name is given which returns name of the month when number of the month is passed as an argument. Here is the code I made from the function.
Code:
#include<stdio.h>
char *month_name(int);
int main(void)
{
int n;
printf("Enter");
scanf("%d",&n);
printf("%s",month_name(n));
}
char *month_name(int n)
{
char *name[]={"illegal month","jan","feb","march","april","may","june",
"july","august","september","oct","nov","dec"};
return (n<1||n>12)?name[0]:name[n];
}
Now my questions are:
1. Is name a one-dimensional or two dimensional array? This is what K&R have written
Since the size of the array name is not specified, the compiler counts the initializers and fills in the correct number.
What will the compiler fill with: 13 or something else?
2. Another quote from K&R in the same section
This is an ideal application for an internal static array.
Why is this an ideal application of static. I did the program without static and it runs fine?
3. Although the program didn't give any errors it gave a warning
Code:
Warning 1 warning C4996: 'scanf' was declared deprecated
What's wrong with the compiler?
4. I'm fed up of asking this question and now feel awkward to ask it but what is the difference between
char *s
char s[10]
I'm not able to visualize the storage of s in the former one. I've checked it in many sites but still I'm not able to get it.
Thanks for reading this huge post.