简体   繁体   English

声明二维数组并找到sizeof:C编程

[英]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? 为具有5行10列的二维数组A编写声明,其中每个元素都是长度为20的字符串。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. 这是在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? 我不知道我是否在误解您的问题,或者是否真的像查找您的5 * 10 * 20字符数组的大小一样简单? The sizeof type char is 1-byte per- char . sizeof运算类型char是1字节per- char Thus 5 * 10 * 20 = 1000 (as compared to say a type int which would normally be 4-bytes per- int ). 因此5 * 10 * 20 = 1000 (相比于说型int这通常是4个字节per- int )。

To determine the size within the scope where the array is statically declared you can use the sizeof operator, for example: 要确定静态声明数组的范围内的大小可以使用sizeof运算符,例如:

#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; 要将数组中的每个元素设置为' 'space ),您有两个选择(1)在数组中的每个元素上循环。 or (2) using memset . 或(2)使用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. 如果您需要字面意义上的空白字符串,则需要在每个字符串中添加一个以n结尾的字符作为最后一个字符。 When using the loop above, that can be accomplished by replacing c[i][j][k] = ' '; 使用上面的循环时,可以通过替换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: 下面的代码使用三元运算符在每个字符串中写一个nul字符zero )作为最后一个字符:

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

To use memset (don't forget to #include <string.h> ): 要使用memset (不要忘记#include <string.h> ):

memset (c, ' ', sizeof c);

To null terminate following the use of memset , an abbreviated loop can be used: 要在使用memset终止为null,可以使用缩写循环:

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. 首先为您的20个字符串声明一个类型。

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. 因此,A是具有5行10列的5型字符串类型的变量。 Each element in the array is of type mystring which as a string that can hold 20 characters. 数组中的每个元素的类型均为mystring,该字符串可以容纳20个字符。 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. mystring可以容纳的最大字符串长度当然是19个字符,因为您需要为空终止符保留一个字符。

An array A that has 5 rows and 10 columns and in which each element is a character string of length 20. 具有5行10列的数组A ,其中每个元素都是长度为20的字符串。

  1. What is the sizeof A? 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. 数组A的大小是将其列乘以其行,并将结果乘以数组元素的大小而得到的。 In this case they are strings of 20 characters length. 在这种情况下,它们是20个字符长度的字符串。 Assuming your machine's char implementation is 1 byte, then array A occupies total size of: 假设您计算机的char实现为1个字节,则数组A总大小为:

5 x 10 x 20 = 1000 bytes or 1MB. 5 x 10 x 20 = 1000字节或1MB。

  1. To define such an array in C++, you write within your main , avoiding magic numbers: 要在C ++中定义这样的数组,请在main内编写,避免使用幻数:
 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: 要将其元素初始化为特定值,可以对数组的每个索引使用for循环:
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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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