Initializing 2D char array in C

Similar to this question: 2d array using calloc in C

I need help initializing a 2D char array to be initialized to some value (in this case "0"). I have tried many different methods and I am pulling my hair out. Please let me know what I am doing wrong. This code doesn't work. Thank!

char** init_array() {
        char newarray[5][10];
        int i, j;
        for (i = 0; i < 5; i++) {
                for (j = 0; j < 10; j++) {
                        newarray[i][j] = '0';
                }
        }
        return newarray;
}
char **array = init_array();

      

The errors I get from gcc when I try to compile:

test.c: In function โ€˜init_arrayโ€™:
test.c:12:2: warning: return from incompatible pointer type [enabled by default]
  return newarray;
  ^
test.c:12:2: warning: function returns address of local variable [-Wreturn-local-addr]
test.c: At top level:
test.c:14:1: error: initializer element is not constant
 char **array = init_array();

      

Should it be so?

char newarray[5][10];
char** init_array() {
        int i, j;
        for (i = 0; i < 5; i++) {
                for (j = 0; j < 10; j++) {
                        newarray[i][j] = '0';
                }
        }
        return newarray;
}
char **array = init_array();

      

+3


source to share


3 answers


I think the photos help. Here char newarray[5][10]

. It is a single block of memory consisting of an array of 10 characters and an array of five. You can just clear it with one call memset

.

enter image description here

Here char **array

. He says array

it's a pointer. What is this pointer for? a pointer to a character.

enter image description here

Be aware of pointer arithmetic. If array

is a pointer pointing to a pointer, then (*array)

equals array[0]

, and this is the pointer that array

points to.

What is it array[1]

? This is the second pointer in the array that points to array

.



What is it array[0][0]

? This is the first character that the first pointer points to array

.

What is it array[i][j]

? This is the j-th character of the i-th pointer that it points to array

.

So how are newarray

and related array

?
Just. newarray[i][j]

is the jth symbol of the i-th subarray in newarray

.

So in that sense it's just like array

, but without all the pointers at the bottom.

Who cares? Well, the downside array

is that you have to build it piece by piece. OTOH, the advantage is that you can make it as big as you want when you create it. It doesn't have to live in a known, fixed size.

Remove like dirt?

+10


source


To avoid using the global (like the second code example inserted above) and to avoid using it malloc

, you can define an array outside of your function and pass it like this. You don't need to return anything because the array data changes. Note that you need to define the secondary size of the array in the function signature:

void init_array(char ary[][10]) {
    int i, j;
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 10; j++) {
                ary[i][j] = '0';
        }
    }
}

int main(void)
{
    char newarray[5][10];
    init_array(newarray);
    printf("%c", newarray[1][1]); /* Testing the output */
    return 0;
}

      



This returns '0'.

http://codepad.org/JbykYcyF

+1


source


In our discussion in the comments, here's a quick example of zeroing array values โ€‹โ€‹during declaration. Note, the values #defined

as constants:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXSTR 10
#define MAXLEN 1024

int main () {

    char myarray[MAXSTR][MAXLEN] = {{0}};  /* declare a static array of MAXSTR x MAXLEN */

    /* copy various strings to statically declared storage */
    strncpy (myarray[0],"  This is the first string", strlen("  This is the first string")+1);
    strncpy (myarray[1],"  This is the second string", strlen("  This is the second string")+1);
    strncpy (myarray[2],"  This is the third string", strlen("  This is the third string")+1);
    strncpy (myarray[3],"  This is the fourth string", strlen("  This is the fourth string")+1);
    strncpy (myarray[4],"  This is the fifth string", strlen("  This is the fifth string")+1);

    int i = 0;

    /* print the values */
    while (*myarray[i])
        printf ("  %s\n", myarray[i++]);

    return 0;
}

      

output:

$ ./myar
    This is the first string
    This is the second string
    This is the third string
    This is the fourth string
    This is the fifth string

      

assemblies:

gcc -Wall -Wextra -o myar myarray.c

      

+1


source







All Articles