Thread: equivalent of old C code

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    3

    equivalent of old C code

    this code

    Code:
    unsigned char *fs;
    u32 fssize;
    
    fssize = *(((u32 *)fs)++);
    returns
    Code:
    lvalue required as increment operand
    error with gcc-4.7. I change it to

    Code:
    unsigned char *fs;
    u32 fssize;
    u32 * u32fs;
    
    u32fs = ((u32 *)fs);
    *u32fs = *u32fs++;
    fssize = *u32fs;
    But application does not work as expected. Is conversion true? What is equilavent of this code ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    I suspect that this was intended:
    Code:
    fssize = *((u32*)fs);
    fs += sizeof(fssize);
    My reasoning is that the cast of fs to u32* seems intended to incremented it as one would increment a u32*, i.e., to increase its value by the number of bytes of a u32. At the same time, post-increment was used, so it looks like it is also intended to assign what fs points to to fssize.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    3
    @laserlight thanks,
    Can you converT this
    Code:
    *(((unsigned char *)dest)++) = c;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    I have provided my reasoning for the previous example, so perhaps you can do something along the same lines, assuming that my reasoning was reasonable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    3
    I wrote this. But I am not sure it is true?
    Code:
    *((unsigned char *)dest) = c;
    
    dest += sizeof(c);

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Does it make sense according to the context of the code? Does it work for you, e.g., does it compile and do the tests pass?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are these two pieces of code equivalent?
    By leogoldseed in forum C Programming
    Replies: 7
    Last Post: 09-29-2008, 11:55 PM
  2. equivalent code question need help
    By Mshock in forum C Programming
    Replies: 4
    Last Post: 04-15-2006, 04:18 PM
  3. STL map<> Equivalent
    By nickname_changed in forum C# Programming
    Replies: 1
    Last Post: 02-27-2004, 06:32 AM
  4. %3s equivalent for C++
    By mackol in forum C++ Programming
    Replies: 4
    Last Post: 03-17-2003, 06:10 AM
  5. what's the c equivalent to this asm code
    By *ClownPimp* in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2002, 12:03 AM