0

How to find number of rows in dynamic 2D char array in C?

Nothing from there.

tried with following code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int k = 97;
void foo(char **a)
{
    int i = 0;
    for(i=0; a[i] != NULL; ++i)
      printf("i = %d\n", i);
}

void strcpyo(char* a, char*b){
    int i=0;
    for(i=0;b[i]!='\0';i++){
        a[i]=b[i];
    }
    a[i]='\0';
}

void strcpym(char* a, char*b){
    int i=0;
    for(i=0;b[i]!='\0';i++);
    memcpy(a,b,i+1);

}
void freee(char** ptr){
int i;
        for(i = 0;i < k; ++i)
        {
            free(ptr[i] );
        }
    free(ptr);

}

void alloc(char ***p)
{
    *p = (char **)malloc(k * sizeof(char *));

    int i,j;
    for(j=0;j<k;j++)
    {
       // for(i = 0;i < j; ++i)
        {
            (*p)[j] = (char *)malloc(11 * sizeof(char));
            strcpy((*p)[j],"paicharan");
        }
        //printf("j = %d ", j);
        //foo(p);
    }
}

int main()
{
    char **p;
    alloc(&p);
#if 0
    char **p = (char **)malloc(k * sizeof(char *));
    int i,j;
    for(j=0;j<k;j++)
    {
        for(i = 0;i < j; ++i)
        {
            p[i] = (char *)malloc(11 * sizeof(char));
            strcpy(p[i],"paicharan");
        }
        printf("j = %d ", j);
        foo(p);
    }
#endif
    foo(p);
    freee(p);
    return 0;
}

The code in #if 0 #endif works perfectly, but if I do create arrays in function alloc(char**) it's giving the wrong answer for odd number of rows in array. Can anybody explain why?

ie. for k= odd number it gives out wrong answer but for even number its correct.

Community
  • 1
  • 1
Charan Pai
  • 2,288
  • 5
  • 32
  • 44
  • 2
    Oops, [a three star programmer](http://c2.com/cgi/wiki?ThreeStarProgrammer)! – legends2k Dec 05 '13 at 11:00
  • just wanted to show you both methods so used 3 star :p – Charan Pai Dec 05 '13 at 11:01
  • Your already start with a problem of vocabulary: the things you handle are *not* 2D arrays but pointers to pointers. If you have a chance to avoid such a maintenance nightmare, just use real 2D arrays. The language has them they are clean and much easier to use. – Jens Gustedt Dec 05 '13 at 12:31
  • ya ya.. can i say its a dynamic 2D array ? anyway, my question was something different than what u thinking :D – Charan Pai Dec 06 '13 at 04:40

1 Answers1

2

Your code depends on Undefined Behaviour to work correctly i.e. it'll work only by chance. This has got nothing to do with even or odd count of elements.

In the void alloc(char ***p) function you allocate memory for k pointer to pointer to char: char**. Then you fill all of the k pointers with new valid char* pointers i.e. none of them are NULL. Later in void foo(char **a) you do for(i=0; a[i] != NULL; ++i); since a[k - 1] was non-null, it'll iterate over them correctly. BUT after that a[k] may or may not be NULL, you never know what is in there. Also accessing what is beyond the array you allocated is undefined behaviour (due to out of bounds access).

Making k + 1 elements and setting the kth element to NULL makes this work; make sure you free all of k + 1 elements and not leak the last sentinal element.

Since you told that the code wraped inside the macro works fine, I've ignored that; don't know if there's UB there too. If you're doing this exercise to learn, it's fine. If you are planning to do some other project, try to reuse some existing C library which already gives these facilities.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222