Thread: C prog questions... help please

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    10

    C prog questions... help please

    Hi all,

    I recently have started to learn about c programming and in a job interview I was asked several questions that I am trying to find the answers to...

    Q1 Explain the function of the following code :-

    Code:
    void function(char *str1, char *str2)
    {
                while(*str2++ = *str1++);
    }


    Q2 Why is it necessary for the data pointed to by str1 to have a zero value somewhere in it?

    I have also been asked to write a section of code that will reverse a 16 bit word (of a 16 bit variable) and return the reversed variable. It needs to start like this:

    Code:
     Unsigned int Reverse(unsigned int var1)
    {
     
                your proposed code etc.
     
     }
    
    I'm thinking that using a stack to place each bit on and take off in reverse order might work but im not sure how to split the variable into it's bits.

    I hope that someone can help with these questions and many thanks if you are able to.

    Will

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    Sorry all, I don't know what that is in the corner.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    I'm not trying to just get the answers to get a job... I did c prog a couple of years back and enjoyed it. but since then I have forgotten so much and not done anything with it. I am after the answers so that I can understand them better.

    With the first question I am thinking that the while loop is continuing to increment the pointers together and str1 needs a zero value so that it can reset to the beginning of the memory range or so that it can start at the beginning.... but I don't really know. its just a guess.

    As for the "create a piece of code" question.... I am thinking that I want to take the variable, split it down into its 16 bit word, place each bit onto a stack starting with bit 0, then remove and send the new reversed word back... but I can't think how to even start. I know this should be simple and I will analyse the code to understand what it is doing.

    Please help as it's driving me insane lol

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your Q1 and Q2 are pretty basic: how about you explain what you think the answers are, then people can give you advice on anything you have clearly not understood.

    On the reversing of bits, you might want to start by googling for "C bitwise operations".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Interesting guess on the first one Answer is far more mundane than that. Think about the syntax and behaviour of a while loop (or look it up if you don't remember).

    The bit reversal one -- again, interesting idea! Might be the right way to do it in some other language, but in C you don't have trivial access to a stack data type (could implement your own, but trust me, it's not necessary for this problem ). C doesn't have a type for a single bit, so you'll have to extract the information from the larger types. Yep, go read about bitwise operators. Shifts and masks are your friend.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    With questions 1 and 2... my thoughts are that the while loop is doing a do-while-do action and will only work once for the condition its trying to match. I understand the ++ incrementing and the srt2=srt1 (making srt2 equal srt1). But then, my thinking of while loops is that they will loop as long as the condition is true like str1==str2. but putting it all together like that doesn't make sense to me.

    as for the other question, I have looked at bitwise operations and the closest thing i can find is the "ones complment" function.. ~ .. which inverts all bits. but it doesn't swap bit 0 with bit 15 and bit 1 with bit 14 etc.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you write
    while( (*str2++ = *str1++) != 0 );
    does that help?

    As for the other question, look up all the bitwise operators, like &, |, << and >> as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    tbh, that still doesn't really make it clearer... is it stating a condition to be met? like pointer str2 and str1, when incremented do not equal 0?

    also, ive been thinking and looking at the bit operations question too.. but i cant make a logical method without it becoming epic.

    could someone explain it to me please as i think im becoming tired and frustrated lol

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    what is REALLY frustrating me is that when i did c programming a couple of years ago, I was quite good at it haha. I understood what I was doing (albeit relatively simple) and now I have completely gone back to being a raw beginner. I started self learning java for android app stuff too, but then got stuck on trying to get information from the billion online web pages and still not understanding it.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by willgoodenough View Post
    With questions 1 and 2... my thoughts are that the while loop is doing a do-while-do action and will only work once for the condition its trying to match. I understand the ++ incrementing and the srt2=srt1 (making srt2 equal srt1). But then, my thinking of while loops is that they will loop as long as the condition is true like str1==str2. but putting it all together like that doesn't make sense to me.
    That's fine - it's ok for it not to make sense, you're on the right track though.

    So, remember that in C non-zero is "true" and 0 is "false". So if I did
    Code:
    int i = 4; y = 3;
    
    while (i + y)
       // do stuff
    That'd loop forever. It's legal though, as the expression i+y has a non-zero value.

    So... does an assignment expression have a value? Either it doesn't, in which case that is bad code, or it does. Look it up!

    Quote Originally Posted by willgoodenough View Post
    as for the other question, I have looked at bitwise operations and the closest thing i can find is the "ones complment" function.. ~ .. which inverts all bits. but it doesn't swap bit 0 with bit 15 and bit 1 with bit 14 etc.
    Indeed, it does not. There isn't a "reverse" instruction Just to save you hunting.

    Say if I have an int, its value is 0110101101 in binary. I have access to bitwise AND, OR, shift left and shift right. I want to find out in C if the bottom bit is set. How do I do it?

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I'm not trying to just get the answers to get a job...
    I'm sort of curious as to why you offered this up with no prompting.

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    Smokey,

    I'm understanding that the section of code is incrementing the pointer str1 and str2 together on a continuous loop until it reaches a zero value. hense why it is important to have that zero value or else it will keep going forever.

    to answer your bitwise question... i suppose i could AND it to a 0000000001 int and it will tell me if that bottom bit is set to 1 or not.

    Rags,

    I was concerned that no one was replying to my origional feed because i mentioned that I got these questions in a job interview lol... and I thought that i should let people know that I wasn't trying to just get the answers to score a job haha

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by willgoodenough View Post
    Smokey,
    I'm understanding that the section of code is incrementing the pointer str1 and str2 together on a continuous loop until it reaches a zero value. hense why it is important to have that zero value or else it will keep going forever.
    What I was getting at is that the result of an assignment is the value that is assigned. So when it encounters a 0 in *str1, it assigns it to *str2, so the result of the expression is 0, so the loop ends. It wouldn't terminate if there was no 0 in str1 but there was one in str2. It's a bit more obvious that assignments return the result of the assignment if you think of
    Code:
    i = j = 5;
    to answer your bitwise question... i suppose i could AND it to a 0000000001 int and it will tell me if that bottom bit is set to 1 or not.
    Yep!

    Code:
    unsigned int Reverse(unsigned int var1)
    {
          unsigned int reversed = 0; // reversed number goes here
    
          // in here need a loop which takes 16 bits of var1
          // and writes them in reverse into reversed
    
          // we know we can find out if the bottom bit is set with
          if (var1 & 1) 
             // so then we know we need to put a 1 or a 0 into reversed
    
          return reversed;
    }
    Not very helpful ha!

    So, you need to figure out what happens in that loop. The way I'm seeing it is you examine a bit in var1 then write a bit in reversed. There's more than one way you could do this -- and probably much more efficient ways with clever bit manipulation.
    Time to think about shifts...

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by willgoodenough View Post
    Rags,

    I was concerned that no one was replying to my origional feed because i mentioned that I got these questions in a job interview lol... and I thought that i should let people know that I wasn't trying to just get the answers to score a job haha
    Wow, I totally read past that....sorry.

  15. #15
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    Thanks smokey, I will look into it. its pretty complex for a question and I will have a go.

    thank you for all your help mate, much appreciated

    and Rags.. no worries man, i don;'t use forums much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Prog
    By PreeMaria in forum C Programming
    Replies: 4
    Last Post: 11-08-2009, 10:01 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Replies: 26
    Last Post: 03-20-2004, 02:59 PM
  4. Help me name my prog.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 11-01-2002, 08:22 PM
  5. A few Windows prog questions
    By Garfield in forum Windows Programming
    Replies: 10
    Last Post: 11-05-2001, 07:06 PM