-1

I am trying to make two dimensional dynamic string arrays with some success but for some reason two int variables, which are in fact the number of the rows (array of pointers) and the size of the strings (how long they can be), change to a mysterious value.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

void write (char ***p, int size_r, int size_c)
{
    int i;
    for (i = 0; i < size_r; i++)
    {
        printf("%s", p[i]);
    }
    return;
}

void arr_of_p (char*** p, int size_r, int size_c)
{
    int i;
    for (i = 0; i < size_r; i++)
    {
        p[i] = "helo\n";
    }
    return;
}



int main(void)
{
    int string_n; printf("How many strings: "); scanf("%d", &string_n);
    int string_l; printf("How long are the strings: "); scanf("%d", &string_l); 


    char **s_p = (char**) malloc(string_n*sizeof(char*));
    int i;
    for (i = 0; i < string_n; i++)
    {
        s_p[i] = (char*) malloc(string_l*sizeof(char));

    }
    arr_of_p(&s_p, string_n, string_l);
    printf("%d\n%d\n", string_n, string_l); // for debugging purpose, add breakpoint here. 
                                           //"string_n" and "string_l" will be identical to the value of "i" in "arr_of_p()" for some reason... 
    write(&s_p, string_n, string_l);
    return 0;
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
John Dale
  • 51
  • 1
  • 8
  • 1
    If `string_l` is supposed to be the maximum length of your input strings, and you *meet* that length, then you allocation size for that is off by one. You're not accounting for terminating nullchar room. And Fyi, there is no need for triple indirection in this code that I can see. Also, this leaks memory like a sieve, and you orphan dynamic allocations in `arr_pf_p`. Finally (unrelated), [don't cast `malloc` in C programs](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). It can quite literally do no good, and can easily hide bad. – WhozCraig Oct 28 '17 at 18:49
  • 1
    Your `arr_of_p` function is assigning the same static string `"helo\n"` to all of the pointers in `s_p`. So it doesn't matter that you've allocated space for the strings earlier in the loop in `main` -- all that memory is being leaked. Also, the `char ***p` argument in `arr_of_p` is not what you want. So the `p[i] = ...` line is also wrong (are you sure there's no compiler warning for that?) – Dave M. Oct 28 '17 at 18:53
  • Actually, that's why your int variables are getting stepped on -- `arr_of_p` is writing the address of the static string over their values. – Dave M. Oct 28 '17 at 18:54
  • Yes but i tried it with string_n = 20 and string_l = 10. So obviously "helo\n" has more than enough space for the terminating character. Adding some free() in the end does not solve this problem... – John Dale Oct 28 '17 at 18:54
  • @DaveM. In uni i was taught to do so. I really have no clue now what i should write in arr_of_p instead of char ***p. – John Dale Oct 28 '17 at 18:59
  • I think the shortest way to fix your immediate problem is to change the call of `arr_of_p( &s_p, string_n, string_l )` to `arr_of_p( s_p, string_n, string_l )` (drop the `&` from `s_p`) and change the signature of `arr_of_p` to be `arr_of_p( char *p[], int size_r, int size_c )`. However, it's really important to figure out how to get complete warnings (as Michel says, you should see many of these) and also revisit your understanding of pointers and arrays in C. – Dave M. Oct 28 '17 at 19:16

1 Answers1

1

Please

1) compile your code with the options for warning, and READ THE MESSAGES

t.c: In function ‘write’:
t.c:11:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat=]
         printf("%s", p[i]);
                  ^
t.c:6:40: warning: unused parameter ‘size_c’ [-Wunused-parameter]
 void write (char ***p, int size_r, int size_c)
                                        ^~~~~~
t.c: In function ‘arr_of_p’:
t.c:21:14: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
         p[i] = "helo\n";
              ^
t.c:16:43: warning: unused parameter ‘size_c’ [-Wunused-parameter]
 void arr_of_p (char*** p, int size_r, int size_c)


                                     ^~~~~~

2) provide an explicit example of what seems to go wrong.

3) avoid using common function names like write as a function name.

Michel Billaud
  • 1,758
  • 11
  • 14
  • codeblocks says warnings as soon as you configure it so. See compiler options. See http://wiki.codeblocks.org/index.php/FAQ-Compiling_(errors) – Michel Billaud Oct 29 '17 at 10:21