Thread: list of questions

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    Angry list of questions

    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.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. 13

    2. static (and const!) would be suitable here. The "static" would tell the compiler to make it a "local global", meaning that the name array is initialized at load-time, rather than each time the function is called.

    3. What's wrong is that you are using MS compiler. It is a good compiler, but they have strange ideas about how to make it safe....

    4. char *s is a (usually 4 bytes in typical machines today) storage space for a pointer. There is no defined space to store anything other than the address of some memory. You can do
    Code:
    s = "abc"
    , but you can't use
    Code:
    strcpy(s, "abc");
    without first giving s a memory location to store the string into.
    On the other hand char s[10] is space for 10 characters (or 9 characters, plus the terminating zero if we are talking about about C style strings).
    Edit: In the latter case, you can do:
    Code:
    strcpy(s, "abc");
    but not:
    Code:
    s = "abc";
    since s is an array, and there is no fundamentals in C to copy arrays.

    --
    Mats
    Last edited by matsp; 06-03-2009 at 02:32 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    3. Project -> <program name> Properties

    Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions

    You will see the following
    WIN32;NDEBUG;_CONSOLE

    You need to add:
    _CRT_SECURE_NO_DEPRECATE

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by nonoob View Post
    3. Project -> <program name> Properties

    Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions

    You will see the following
    WIN32;NDEBUG;_CONSOLE

    You need to add:
    _CRT_SECURE_NO_DEPRECATE
    Thanks nonoob for this one. Now the warning has gone.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Is name a one-dimensional or two dimensional array?
    It's a one dimensional array of (char *)'s - Each element in the array points to the beginning of a string, those strings are stored somewhere else entirely.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM