c - Convert an int** array into a char** array of a different size / composition -


i'm trying convert int** array looks below, char** array made number of rows , comma seperated.

1 2 3 4 5 6 7 8 9 3 2 1 

an example of char** array represented [['1','1','2','3','4'], ['2','5','6','7','8'] , ['3','9','3','2','1']] every 0'th index represented row number. i'm not sure how able achieve this, there known methods converting int** this? thanks.

i think needed string, not 2d-array.
can written out string write standard output implementing string simple extension.

#include <stdio.h> #include <stdlib.h> #include <string.h>  typedef struct char_vec {     char *v;     size_t capacity;     size_t used; } sstream;  sstream *ss_new(void); void ss_free(sstream *ss); void ss_putc(sstream *ss, char ch); void ss_puts(sstream *ss, char *str);  int main(void){     int rows = 3;     int cols = 4;     int **array = malloc(rows * sizeof(*array));     for(int r = 0; r < rows; ++r){         array[r] = malloc(cols * sizeof(**array));     }     //set values     memcpy(array[0], (int[]){1,2,3,4}, cols * sizeof(**array));     memcpy(array[1], (int[]){5,6,7,8}, cols * sizeof(**array));     memcpy(array[2], (int[]){9,3,2,1}, cols * sizeof(**array));      //print original array     for(int r = 0; r < rows; ++r){         for(int c = 0; c < cols; ++c){             if(c)                 putchar(' ');             printf("%d", array[r][c]);         }         puts("");     }     //make string of output     char temp[32];     sstream *ss = ss_new();     ss_putc(ss, '[');     for(int r = 0; r < rows; ++r){         if(r)             ss_putc(ss, ',');         snprintf(temp, sizeof(temp), "['%d'", r + 1);//0'th index represented row number         ss_puts(ss, temp);         for(int c = 0; c < cols; ++c){             snprintf(temp, sizeof(temp), ",'%d'", array[r][c]);             ss_puts(ss, temp);         }         ss_putc(ss, ']');     }     ss_putc(ss, ']');     ss_putc(ss, '\0');      puts(ss->v);     ss_free(ss);      //deallocate array     for(int r = 0; r < rows; ++r)         free(array[r]);     free(array);     return 0; }  sstream *ss_new(void){     sstream *ss = malloc(sizeof(*ss));     ss->capacity = 32;     ss->used = 0;     ss->v = malloc(ss->capacity);     return ss; } void ss_free(sstream *ss){     free(ss->v);     free(ss); }  void ss_putc(sstream *ss, char ch){     ss->v[ss->used++] = ch;     if(ss->used == ss->capacity){         char *temp = realloc(ss->v, ss->capacity += 32);         if(!temp){             ss_free(ss);             fprintf(stderr, "fail realloc @ %s\n", __func__);             exit(exit_failure);         }         ss->v = temp;     } }  void ss_puts(sstream *ss, char *str){     while(*str)         ss_putc(ss, *str++); } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -