few questions

This is a discussion on few questions within the C Programming forums, part of the General Programming Boards category; Hello to everyone, I was doing some questions on C but don't know the answers to them, so I just ...

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

    few questions

    Hello to everyone,
    I was doing some questions on C but don't know the answers to them, so I just want to cross check them here. Here are the questions.

    1.
    Code:
    char *f()
    {
    char *s=malloc(8);
    strcpy(s,"goodbye");
    }
    main()
    {
    char *f();
    printf("%c",*f()='A');
    }
    I'm completely doubtful regarding this one. In the function f() which returns pointer to char, s is allocated space and string "goodbye" is copied to it. But in main() how is the function getting called and how is the output to it is 'A'. Also if I change the printf to
    Code:
    printf("%c",*f());
    the output becomes the first letter of the string getting copied i.e in this case 'g'.
    Can someone please explain this code to me?

    2. What does int *x[](); means ?
    I think it's an array of pointers to function returning int. Am I correct? Also is there a specific procedure to be followed for complicated declarations like this one and others too?

    3.What is the output of the following ?
    Code:
    int i;
    i=1;
    i=i+2*i++;
    printf("%d",i);
    The output to this is 4 but is this not an undefined behaviour? I think it's not

    Thanks in advance for your responses.
    Last edited by BEN10; 07-05-2009 at 11:40 PM.
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. %c means to print a character. * means to dereference. f() is a function call. This particular function call returns a pointer to character, which can be (and is!) dereferenced with *. The value of an assignment is the thing that is being assigned, which is why you can do something like a = b = 1; the value of b = 1 is 1, and that value is then assigned to a.

    2. int *x() would be a function, x, returning a pointer-to-integer. Now you have an array.

    3. The value of i is undetermined by this code.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    1. Now I understand that 'A' was assigned to it, but for this printf why the output is so:
    Code:
    printf("%c",*f());//the output becomes the first letter of the string getting copied i.e in this case 'g'.
    f() should return pointer to char * but its actually not returning anythig so how is the output so in this case.
    2. So what it is actually?
    3. How is the value of i undetermined, sorry for the mistake in printf it's actually
    Code:
    printf("%d",i);
    By the way thanks for the quick reply.
    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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In 1, I guess you're getting "lucky" that s is being returned. (I just read it as such in spite of it not being there, when I first read the code.)

    In 3, the piece of code "i=i+2*i++;" is utterly meaningless. i is allowed to be anything the compiler writers want it to be at the end of that line.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    For 3, here is what I think is going on. i=1 assigned, now i++ is replaced by 1 and the value of i is getting 2, so it becomes like this
    i=2+2*1
    which gives i=4.
    So where is it meaningless. It's not something like i=i++, what is the vlaue of i at the end of the line is ambiguous.

    Btw what about the 2nd question?
    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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't know what you don't understand about my comment to #2, so I don't know how to make it more explicit.

    As to #3, it is exactly i = i++. The 2* doesn't change anything, as neither the 2* nor the assignment part is supposed to/required to happen before the "++". That's what postfix ++ means, after all; do other stuff, then add 1.

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    You mean everything incuding the ++ operator in expressions is undefined. For eg
    Code:
    i=2;
    j=i++;
    will this also be undefined?
    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

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BEN10 View Post
    You mean everything incuding the ++ operator in expressions is undefined. For eg
    Code:
    i=2;
    j=i++;
    will this also be undefined?
    No. j will be 2 and i will be 3. But:
    Code:
    i = 2;
    i = i++;
    is problematic. The compiler is allowed to add 1 to i at any time between accessing the value of the variable to use in the expression and starting the next "line" of code. So it must first access the value of i (which is 2); it must then, in some order, assign 2 to i, and add one to i. If it does it in that order, i ends up as 3. If it does it in the opposite order, i ends up as 2.

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by tabstop View Post
    2. int *x() would be a function, x, returning a pointer-to-integer. Now you have an array.
    Do you mean int *x[]() is an array of functions returning pointers to int? If yes what would this declaration mean
    Code:
    int *x()[]
    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

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BEN10 View Post
    Do you mean int *x[]() is an array of functions returning pointers to int? If yes what would this declaration mean
    Code:
    int *x()[]
    It would mean "invalid declaration". (If it meant anything, it would mean function returning an array of pointer-to-int, but a function can't return an array.)

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    One last question how did you say that int *x()[] is an array of functions returning pointers to int. Is there a specific way by which one determines complicated declarations? Please help me in this one as I get confused in complicated declarations very easily. And yes please do explain it a bit.
    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

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I suppose I should mention first off that you can't have an array of functions either. (An array of function pointers, fine. Array of functions proper, no.)

    Let's look at basically the only legal way to combine these things:
    Code:
    int *(*x[5])();
    This says "I am trying to declare something called x. The *x[5] means I have an array of pointers to whatever is outside the *. (fn1) The thing outside the * is int *(), meaning a function returning an int*."

    (fn1) This is a little strange notation pretty much required for function pointers, since the grouping doesn't work otherwise. If I want an array of pointers-to-int, I just need int *x[5] without any parentheses, 'cause the thing outside the * is just int. But the function call parentheses bind to the thing immediately to the left, so I need to make sure the array part is inside parentheses so that the () will bind to the whole thing -- if I just did int* *x[5]() that would bind as an array of five functions returning int** instead of what I want. [This is slightly different than when I want a pointer to an array: int (*x)[5] -- here the thing outside the * is int[5], so I get a pointer to an array, not an array of pointers. Same deal, the [5] at the end will bind with the thing immediately to the left.]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  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. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21