简体   繁体   中英

Declaration for a two dimensional array and finding sizeof: C programming

Write a declaration for a two dimensional array A that has 5 rows and 10 columns and in which each element is a character string of length 20. What is the sizeof A?

Part II: Write statements to set each element in the array to a string of blanks.

This is first year programming so everything is very basic. This is in C.

Thanks for any help.

Here's my attempt for part I

char c[5][10][20];

Here's my attempt for part II

int x,y;  
for (x=0;x<5;x++) {
     for (y=0;y<10;y++) {
         c[x][y]=" "; }}

I don't know if I'm misreading your question or whether it really is as straight forward as finding the size of your 5 * 10 * 20 character array? The sizeof type char is 1-byte per- char . Thus 5 * 10 * 20 = 1000 (as compared to say a type int which would normally be 4-bytes per- int ).

To determine the size within the scope where the array is statically declared you can use the sizeof operator, for example:

#include <stdio.h>

int main (void) {

    char c[5][10][20];

    printf ("\n sizeof c : %lu\n\n", sizeof c);

    return 0;
}

Use/Output

$ ./bin/sizeof_3d

 sizeof c : 1000

To set each element in the array to ' ' ( space ), you have a couple of options (1) a loop over each element in the array; or (2) using memset . To use a loop:

size_t i, j, k;
...
for (i = 0; i < 5; i++)
    for (j = 0; j < 10; j++)
        for (k = 0; k < 20; k++)
            c[i][j][k] = ' ';

If you need a literal string of blanks you will need to add a nul-terminating character as the final character in each string. When using the loop above, that can be accomplished by replacing c[i][j][k] = ' '; with the following that uses a ternary operator to write a nul-character ( zero ) as the final character in each string:

            c[i][j][k] = k + 1 == 20 ? 0 : ' ';

To use memset (don't forget to #include <string.h> ):

memset (c, ' ', sizeof c);

To null terminate following the use of memset , an abbreviated loop can be used:

size_t i, j, k;
...
memset (c, ' ', sizeof c);
for (i = 0; i < 5; i++)
    for (j = 0; j < 10; j++)
        c[i][j][19] = 0;

Let me know if you have further questions.

First declare a type for your 20 character strings.

typedef struct mystring {
    char str[20];
} mystring;

Then you can declare your variable A LIKE THIS:

mystring A[5][10];

So, A is a variable of type 5 string which has 5 rows and 10 columns. Each element in the array is of type mystring which as a string that can hold 20 characters. The maximum length of a string that a mystring can hold is of course 19 characters since you need to reserve one character for the null terminator.

An array A that has 5 rows and 10 columns and in which each element is a character string of length 20.

  1. What is the sizeof A?

The size of array A we get by multiplying its columns by its rows and the result of it multiplying by the size of the elements of the array. In this case they are strings of 20 characters length. Assuming your machine's char implementation is 1 byte, then array A occupies total size of:

5 x 10 x 20 = 1000 bytes or 1MB.

  1. To define such an array in C++, you write within your main , avoiding magic numbers:
 size_t rows_number = 5;
 size_t columns_number = 10;
 size_t element_size = 20;

 char c[rows_number][columns_number][element_size];
  1. To initialize its elements to particular value, you could use for loops for each index of the array:
char initial_value = ' ';

for (i = 0; i < row_number; i++) {
    for (j = 0; j < column_number; j++) {
        for (k = 0; k < element_size; k++) {
            c[i][j][k] = initial_value;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM