keronhospital.blogg.se

Next space rebels loopings
Next space rebels loopings











next space rebels loopings

") First of all, lets inspect what exactly is the for-loop supposed to do! (The post style is motivated by Donald Knuth's phrase "Science is what we understand well enough to explain to a computer. I'll try to explain all necessary aspects by referencing the ISO/IEC:9899 (emphasis mine)- C99 standard. Ritchie would say: Let's do it with the power of assembly language and the convenience of … assembly language. This value is the value of expression *p. This is equivalent to testing *p != '\0' or *p != 0 as stated above.ġ When *p evaluates then the value of *p is fetched from memory. When p reaches the end of the string and finds the first '\0' character, the expression *p returns 1 a zero value. So, the expression *p in loop just check that the decimal equivalent of character at the memory address pointed by p is either a zero or non-zero. In C, strings are terminated by a null character '\0'.Įvery character has a decimal equivalent. What does the *p do in the following loop? Where expr is any expression at all, the compiler essentially acts as if it had been written as if((expr) != 0) When C requires the Boolean value of an expression, a false value is inferred when the expression compares equal to zero, and a true value otherwise.

next space rebels loopings

I understand you are on precipitous learning curve at the moment with pointers in C, but eventually you'll be able to say "I C the point"īefore diving in, I would like to state a simple rule in C regarding an expression On the 3rd line I added the chars of your string for reference.Normally you would see garbage (seemingly random values)

next space rebels loopings

Then you see a few more null characters because I built in debug mode and the compiler zero-initialized. After that you have the string's null character 0x00. The last char in your string is t with hex value of 0x74.

  • On the 2nd line you see the hex values of the letters in your string.
  • That's where p points to at the start of the for loop.
  • The hex value at the first line is the address (64bit) of this memory block.
  • Here is a diagram of the str string, the indexes (offsets from the start) of each character and the values at each index :įor completeness and after a request at the comments here is what the debugger sees in the memory block that str occupies : 0x00007fffffffe6a0:Ġx53 0圆f 0圆d 0圆5 0x20 0x54 0圆5 0x78 0x74 0x00 0x00 0x00 0x00 0x00 0x00 0x00 Is equivalent to these for (p = str *p != '\0' p++)Īnother name for the null terminating character is sentinel or according to Donald Knuth "dummy value" (Art of Computer Programming, Volume 1). That marker is the null character with (ASCII) value of zero. According to Dennis Ritchie "C treats strings as arrays of characters conventionally terminated by a marker". *p dereferences p and returns the char, p points to. The for loop will terminate if whatever lies between the two in the statement is zero (false).













    Next space rebels loopings