32070

Multidimensional array as a flat array problem

I'm looking at two lines of code that somebody wrote and there is an exception in the 2nd one, however I don't understand why.

char** array = (char**) new char [2] [6]; std_strlprintf(array[0],6,"[%d]", num);

std_strlprintf is a Brew function that writes formatted output to a string. (num is an integral value which is 0)

Why is there an exception with this code, what's wrong with accessing the first elelment of the array as buff[0]?

<hr>

EDIT: sorry there was a typo in my initial posting. Its corrected now. THis is the code that has the exception.

Answer1:

Two-dimensional array is not the same as array of pointers.

Your first statement creates two arrays of six chars each as a single memory block. Replace that statement with:

char (*array)[6] = new char [2][6];

and you'll be all set with your second statement. Don't forget to

delete [] array;

Edit 0:

Huh, I should've known :) To your question in the comment:

How should I subsequently pass array to a function that takes a char** as a parameter?

You don't. Not in this form. If you are building a list of parameters to some C API like execve(2), you have to go all the way with two-stage initialization:

// prototype of the function to call void my_fancy_func( int argc, char* const argv[] ); char** my_argv = new char*[my_argc]; for ( i = 0; i < my_argc; i++ ) { my_argv[i] = new char[arg_buffer_size]; snprintf( my_argv[i], arg_buffer_size, "%d", i ); } my_fancy_func( my_argc, my_argv );

Recommend

  • Antd - is there anyway to change background color of the Card title?
  • how to load an image using sdl from memory (c++)
  • How can I test TCP socket status in Perl?
  • Android: Alternative to onSensorChanged? no ACCELEROMETER data when phone is lying on its back on a
  • Why nullable types will not be equal in this case?
  • Accessing variables in other classes (Java)
  • what is the use case for scala List's companion function returning GenericCompanion?
  • Excel VBA Delete Row If Misspelled Word
  • Int to char conversion rule in C when int is outside the range of char
  • 'Set' in Components of Cake PHP
  • Drawing a polygon over the entire map
  • Android Google Maps: Polygon or shape that hides entire map
  • Purescript types for buildQueryString function
  • Passing array length to function
  • Performance difference between accessing local and class member variables
  • Access PCF DEV from external machine on same network as host
  • Broadcast advanced indexing numpy
  • unescape special characters in ajax response data
  • Passing a Scala type to a function
  • touchstart in JavaScript no longer returns TouchList
  • Listview - list item selection and initial setting (custom made items)
  • TextBox AutoCompleteStringCollection Suggest
  • Allocating a 2D contiguous array within a function
  • How do i disable a text box within an iframe
  • The symbol you provided is not a function
  • possible limitation of implode function in PHP
  • c++ using primitive types as a base class
  • SqlCommand back up Database
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • What Makes These Two Array Adds Different?
  • Outputting SharePoint Hyperlink Column as URL
  • In matplotlib, how do you change the fontsize of a single figure?
  • blade.php method outputting it's result to the form
  • xcode don't localize specific strings
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Yii2: Config params vs. const/define
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Symfony2: How to get request parameter
  • GridView Sorting works once only
  • WPF Applying a trigger on binding failure