Thread: Not sure what this code means.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    4

    Not sure what this code means.

    Hello,

    I am very new to C (as in, I started last week), and am having trouble with this piece of code.

    Code:
    if (b >= sizeof(unsigned int)) b = 0;
    *(ptr + b) = c;
    Elsewhere in the code, b is set to equal 2. My question is: when "sizeof(unsigned int)" is written, what does that refer to? The largest number that can be assigned to an unsigned int? And if the statement is false, then does b stay equal to 2?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    sizeof() returns the number of bytes that a type or variable occupies in memory.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    For your second question, yes, b stays equal to 2. In general, when the value of a variable doesn't change it stays the same.

    For your first question, sizeof returns (at compile time) the size of a type, relative to the size of the char type (sizeof(char) is 1, by definition). See http://en.wikipedia.org/wiki/Sizeof. On x86, sizeof(unsigned int) is usually 4, since unsigned int is 32 bits and char is 8 bits. I say "usually" because int can be as small as 16 bits, and older x86 compilers actually had 16-bit ints. In that case, sizeof(int) would be 2.
    Last edited by christop; 06-15-2012 at 06:03 PM. Reason: pointed to a better reference page

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    With that code, presumably ptr is either an array with sizeof(unsigned int) values allocated, or a pointer to something equivalent (such as malloc'ed memory of suitable size, or - with suitable type conversion - to an actual unsigned int).

    All the code is doing is ensuring that b is a valid index into that array, and then assigning an element of that array.

    As christop sort-of said, sizeof() is a compile-time operator that gives the size of its arguments, relative to char. The result depends on compiler and compiler settings. However, does not vary over time in a running program (as it is, in effect, a compile-time constant).

    On most real-world architectures (i.e. target systems and their compilers) sizeof(unsigned int) is more than 1. So, if b has a value of 2, it will not be changed on those architectures. However there are some less commonly used architectures for which sizeof(unsigned int) is 1 - not every computing device is compatible with x86. On such systems, that code will change the value of b to zero.

    "if" statements are like that ....
    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
    Jun 2012
    Posts
    4
    Thanks! That clears it up.

    One more thing- for " *(ptr + b) = c; ", am I correct in assuming that whatever result comes from (ptr + b) now becomes c? And if so, what happens to the original value of c (it was given an integer value later on in the code)?

    I guess I'm just kind of confused on how to do addition on pointers.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In C, assuming ptr is a pointer or an array (which is generally necessary, otherwise the expression won't compile) *(ptr + b) is equivalent to ptr[b].

    So the statement "*(ptr + b) = c;" and "ptr[b] = c;" are functionally equivalent.

    The code, as you have given it, does not change the value of c (unless the memory occupied by ptr[b] and c overlap).

    If c has not been initialised before your code fragment is executed, then accessing its value has undefined behaviour. You need to ensure that any variable is initialised before executing code that accesses its value.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this code means?
    By junkeat90 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2008, 05:03 AM
  2. Is there a code that means a integer is empty?
    By Roy01 in forum C++ Programming
    Replies: 13
    Last Post: 10-22-2006, 04:06 AM
  3. What does this code means??
    By dianazheng in forum C Programming
    Replies: 13
    Last Post: 10-12-2004, 09:45 PM
  4. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM