70670

What is happening in this C/Arduino code?

Question:

I'm new to C and Arduino development and wondering what's going on here. The code is supposed to print the response from an HTTP request, but instead it cuts off after around 300 bytes.

static void my_callback (byte status, word off, word len) { Ethernet::buffer[off+300] = 0; // <-- Serial.print((const char*) Ethernet::buffer + off); // <-- }

In Javascript, Ethernet::buffer[off+300] = 0 would mean you're assigning a value of 0 to something in an object or array, at position [off+300]. Why is this being done here <em>before</em> the result is returned, or at all?

Next, the value of Ethernet::buffer is added to the value of off (which is a number). So the result should be a number, but instead it's a string.

Any insight into what is going on here would be really appreciated. Thanks.

Source: <a href="https://github.com/jcw/ethercard/blob/master/examples/webClient/webClient.ino" rel="nofollow">EtherCard examples</a>

Answer1:

The assignment of 0 makes sure the string is terminated at 300 characters after off. In C and C++ basic strings are represented as arrays of characters, and use a character with the value 0 to indicate end of string.

This can be a protection against printing too much on the console, for instance.

The addition on the print line is pointer arithmetic, it's not "a number" (or, under the hood of course it's a number, that's all computers deal with, but semantically there's a difference). Adding a number to the address of a string in C (and C++, here) gets you the suffix, i.e. it skips that many characters into the string.

Recommend

  • How to redirect user to Ethernet Settings screen in android programmatically?
  • Parse blocks in curly brackets
  • Can't run IBM worklight app in real device android
  • Writing a device driver for Platform Bus in Embedded Systems?
  • OpenMP for dependent variables
  • Xcode launch app error in Mac 10.9 Mavericks
  • C++ String tokenisation from 3D .obj files
  • Determining the length of a read stream in node js
  • How can I count unique terms in a plaintext file case-insensitively?
  • Programmatically Update Linked Named Range of excel object in MS Word (2007)
  • XBee Linux Serial Port on Rasberry Pi
  • Arduino making decision according to a packet received from serial port
  • incomplete type 'struct' error in C
  • jQuery: How to AJAXify WordPress Search?
  • Invalid Date on validation Date of js
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • Using a canvas object in a thread to do simple animations - Java
  • C: Incompatible pointer type initializing
  • Remove final comma from string in vb.net
  • Declaring variable dynamically in VB.net
  • Blackberry - Custom EditField Cursor
  • copying resource to sdcard gives a damaged file in android
  • Body moving without any force applied? (Box2d)
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • Join two tables and save into third-sql
  • Convert array of 8 bytes to signed long in C++
  • php design question - will a Helper help here?
  • Linker errors when using intrinsic function via function pointer
  • AngularJs get employee from factory
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Android Google Maps API OnLocationChanged only called once
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Authorize attributes not working in MVC 4
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal