ANSI C work with 2dim array throught pointers -


long long time ago i've played c lot forgot everything. trying solve easy tasks , failed.

i'd write function takes 2dim array or char* , print it. code buggy , not understand why. understand products pointer 2dim array, increasing i * sizeof(char**) pointer sub-array, , increasing sub-array pointer pointer char block. seems code looking different memory block.

about products array - know has n rows , 2 columns.

#include <stdio.h> #include <string.h>  void print(char*** products, size_t rows, size_t cols) {     size_t i;      for(i = 0; < rows; i++) {         printf("col1: '%s. col2: %s'\n",             (products + * sizeof(char**)),             (products + * sizeof(char**) + sizeof(char*))         );     } }  int main(void) {     const char* a[][3] = {{"abc", "1"}, {"def", "2"}, {"ghi", "3"}};     print((char***)a, 3, 2);      return 0; } 

you've mixed regard columns , rows. fix that, rid of 3 star nonsense, make function accept variable amount of columns , end this:

#include <stdio.h>  void print(size_t rows, size_t cols, const char* products[rows][cols])  {   for(size_t r=0; r<rows; r++)    {     for(size_t c=0; c<cols; c++)     {       printf("col%zu: %s. ", c, products[r][c]);     }     printf("\n");   } }  int main (void)  {   const char* a[3][2] = {{"abc", "1"}, {"def", "2"}, {"ghi", "3"}};   print(3, 2, a);    return 0; } 

and that's that, no need complicate futher.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -