OpenersNotesSection(Spacer1)Details

MathLink has the ability to convert the current readable data on the link to types other than its link representation on the fly. MLBytesToGet() converts the current data to a character string representation.

MLBytesToGet() returns 0 in the event of an error and a nonzero value if the function succeeds.

Use (NonWLLink[MLError(), paclet:ref/c/MLError]) to retrieve the error code if MLBytesToGet() fails.

MLBytesToGet() is declared in the MathLink header file (RefLink[mathlink.h, paclet:ref/file/mathlink.h]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (1)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

#include <stdlib.h>
#include <string.h>
#include "mathlink.h"

/* check the incoming string size to allocate enough memory to store the string */

void f(MLINK lp)
{
	int incoming_bytes;
	char *string;
	const char *mlstring;

	switch(MLGetType(lp))
	{
		case MLTKSTR:
			if(! MLBytesToGet(lp, &incoming_bytes))
				{ /* unable to get the size of the string */ }

			string = (char *)malloc(incoming_bytes + 1);
			if(string == (char *)0)
				{ /* memory allocation failed */ }

			if(! MLGetString(lp, &mlstring))
				{ /* unable to get the string from lp */ }

			memcpy(string, mlstring, incoming_bytes);

			*(string + incoming_bytes) = '\0';

			break;
		/* ... */
	};

	/* ... */
}

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(NonWLLink[MLGetNext(), paclet:ref/c/MLGetNext])  ▪  (NonWLLink[MLGetType(), paclet:ref/c/MLGetType])  ▪  (NonWLLink[MLGetData(), paclet:ref/c/MLGetData])}})}})