-
In fact, if you have read "C Expert Programming", it is very simple, and this problem has been carefully described above. It's stored in memory as shown in the figure. n[0][5] is actually *(*n+0)+5), which is the sixth cell.
So the output is: y. In the same way, you will know why n[0][6] is i.
The second question: because it starts from 0, there are 11 in total, so the subscript can be from 0 to 10. A multidimensional array is actually an array of arrays, char n[11][5]; In fact, it is equivalent to storing 11 strings, and the length of each string is 5, so you only need to use a subscript to reference.
For example, the value of n[1] is actually pointing"yi"pointers. n[1][4] Why not e, in fact, is the same reason, this is left to you as an exercise. Hint:
string'\0'End. Hope it helps.
-
First of all, I would like to remind you that a=0, b=5 and a=0, b=6 have overflowed, and luckily you have here, the space allocated by char n[11][5] is not completely out of bounds in the case of the overflow above, if you have this kind of operation.
n[11][5] = 0 then the problem can get worse, C++ multidimensional arrays, theoretically, the compiler will end up assigning a segment.
Linear space to accommodate the data determined by the specified dimension, the subscript operation will calculate the true offset by itself, and your overflow operation gets y because the compiler actually references the 6th element of the space allocated by n 55*sizeof(char) after calculation, i.e"yi"Y, very dangerous operation Please be careful.
I didn't understand much about the following questions.。。。 n[1][4] is'\0'It's not'e', your way of writing each string corresponds to one dimension, so let's show you what memory looks like.
char [55] = , equivalent, so it's easier to understand.
-
You can follow the steps below to define a two-dimensional array and output:
1 Define a two-dimensional pointer.
2 Determine the number of array dimensions, i.e., the number of rows and columns.
3. Apply for several 1D pointer size spaces in the row, and assign values to 2D pointers.
4 For each one-dimensional pointer, apply for a space with several elements in the column and assign a value to the corresponding one-dimensional pointer.
5 Enter or assign data.
6 Using a double cycle, the range of storage cells and output them one by one.
7 One by one, release the memory of the 1D pointer on the spring mill.
8 Free up memory on the 2D pointer.
Take an integer two-dimensional array as an example:
In the following **, the number of rows m and the number of columns n of the two-dimensional array are first entered, and then m*n integer data is entered and stored in the base bucket of the dynamic two-dimensional array. Finally, the element values of all 2D arrays are output.
-
There are two ways for a two-dimensional array to assign a value to a one-dimensional array:
1 Assign values to the corresponding target elements one by one.
In all cases, you can customize the rules to iterate over the 2D array and assign a value to the corresponding position of the 1D array. The offset of the one-dimensional array is calculated from the row and column values of the two-dimensional array, and this part of the algorithm can be designed at will and has a high degree of flexibility.
For example, copying the first column of a two-dimensional array is called a separate one-dimensional array, which can be written:
int a[100][100], b[100];
int i;
The two-dimensional array assignment operation can be written here, regardless of the topic, and omitted.
for(i = 0; i < 100; i ++
b[i] = a[i][0];The first column is the 0 column, and the C subscript starts with 0.
2 Replication as a whole.
The elements in the two-dimensional array are stored continuously, and if you want to assign a continuous set of data from the two-dimensional array to the one-dimensional array, you can directly call the memcpy function. The function is declared in the cstring in the form of .
void* memcpy(void *dst, void *src, size_t length);
The function is to copy data with length bytes from the SRC address to the DST address.
For example, if you copy the second and third lines (these two lines are sequential) into a one-dimensional array, you can write:
int a[100][100],b[200];
Assignment, omitted.
memcpy(b,a[1],sizeof(b));Replication as a whole.
-
An array of char characters that stores integer numbers, which are actually ASCII codes for a certain character, such as 41--a, 42---b, etc. However, if the range of characters that can be displayed in ASCII is exceeded, garbled characters will be displayed.
It is recommended to replace char b[3]; Changed to char *b[3].
-
char b[3];The elements of b are char, and 456 and 789 are overflowing for integers of type char.
b is an array of type int.
int b[3];
int i;
for(i=0;i<3;i++)
b[i]=100*a[i][0]+10*a[i][1]+a[i][2];
So b is it.
-
ptr=arr;
In fact, you can assign values like this, there is no problem.
Since arr is the array name of a two-dimensional array, it 636f707962616964757a686964616f31333264626539 (arr) itself represents the address of line 0 of this two-dimensional array.
The key is not here, but in the while loop statement of your program, i++;
First of all, ptr points to an address, so ptr+i is also an address, so you can't use the address operator here"&"Finish. The second problem is that i adds itself from 0 to 6, which actually has 7 numbers, which is significantly longer than the length of the arr array, which is not correct. Another problem with this cycle is that it's an abnormal cycle in itself (think for yourself).
Here's what I helped you change**, I hope it will help you.
#include
void main()
-
i++;I don't quite understand this, I'm also a newbie, I've been in this situation too, I'm doing this inside:
#include
void main()
Hope it helps you a little.
-
It is possible to forcibly transcribe it.
Change,ptr=(double *)arr,But there's still a problem with your program:
Change to while(i!=6 &&scanf("%lf", ptr+i))
-
for( i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("l%f",a[i][j]);
I'm also a newbie and don't know if it's the right thing to do.
-
First, the array subscript in C++ starts at 0, which means that your array can only go up to knot[5][5], and then it crosses the boundary.
Second, when using {} to initialize, you can only include the value in {}, and you can't have the words knot[1][2]= in it.
The correct way to do it as you intend should be int knot[6][6]=,
-
The C++ subscript starts at 0, so you can't use a subscript greater than or equal to 6, and secondly, it's not assigned like this, you can do that:
int arr[3][3] = ;
If each initial value in the array is the same, then you can just do it :
int arr[3][3] = ;
-
You can follow the steps below to define a two-dimensional array and output:
1 Define a two-dimensional pointer.
2 Determine the number of dimensions within the array, i.e., the number of rows and the number of columns.
3. Apply for several 1D pointer size spaces in the row, and assign values to 2D pointers.
4 For each one-dimensional pointer, apply the space of several elements in the column and assign a value to the corresponding one-dimensional pointer.
5 Enter or assign data.
6 Using a double cycle, the range of storage cells and output them one by one.
7 Free up memory on the 1D pointer one by one.
8 Free up memory on the 2D pointer.
Take an integer two-dimensional array as an example:
In the following **, the number of rows m and the number of columns n of the two-dimensional array are first entered, and then m*n integer data is entered into the dynamic two-dimensional array. Finally, the element values of all 2D arrays are output.
-
With a double loop will do the trick:
#include
void main()
row, col;
printf("Please enter the values of the array elements (3 rows and 4 columns): n");
for (row=0; row<3; row++)for (col=0; col<4; col++)scanf("%d", &a[row][col]);
printf("The array you enter is:");
for (row=0; row<3; row++)
-
1 Define a two-dimensional pointer.
2 Determine the number of array dimensions, i.e., the number of rows and columns.
3. Apply for several 1D pointer size spaces in the row, and assign a value to the 2D pointer.
4 For each one-dimensional pointer, request the space of several elements in the column and assign a value to the corresponding one-dimensional pointer.
5 Enter or assign data.
6 Using a double cycle, the range of storage cells and output them one by one.
7 Free up memory on the 1D pointer one by one.
8. Free up the memory on the 2D pointer to assign and output the 2D array elements.
-
There are two cases of assignment for two-dimensional arrays: du
1. Initial assignment at the time of definition.
int a[2][4]=,;2. After definition, the DAO assigns a value to the array elements.
int a[2][4];
int i,j;
for( i=0;i<2;i++ line} After the above assignment is completed, the content is the same as the initial assignment.
The output of a two-dimensional array can only be traversed through the two-dimensional array line by line, and each element can be output one by one. Such as:
Pick up the front**.
for( i=0;i<2;i++ line printf("");After a line is output, the line break is displayed}
-
This should not be a big problem, but you can also think about it and think about it.
-
With a double cycle bai will do
#include
void main()
row, col;
printf("Please enter the value of the array element (3 rows and 4 columns): n");
for (row=0; row<3; row++)for (col=0; col<4; col++)scanf("%d", &a[row][col]);
printf("The number DAO group you entered is:");
for (row=0; row<3;row++) Test Results:
-
1. The two-dimensional array is an array of a[n][n] of the form of a[n][n], the first n is the number of rows, and the second daon is the number of columns, that is, the subscript, for example, I want to define back to a three.
A three-column integer two-dimensional array can be defined as follows: int a[3][3]=;
2. For ease of learning, we abstract the array into the following form 1 2 3
That is, three rows and three columns of data, 1 has an address of a[0][0] (note that the array subscript starts with 0), 2 has an address of a[0][1], the second row has a[1][x] (0 < = x <= 2), and so on.
3. So take each value as long as you give the corresponding subscript to take out the value, for example, if I want to take out the data in the second row and the second column, I can take int b = a[1][1]; The same is true for assignments, if you need to assign a value to each element loop you need two layers of for loops, for example.
for(i = 0;i < 3; i++)
-
Nested loops, with two layers of loops will do.
One-dimensional arrays hold the same values, but in the case of matrices, determinants. >>>More
The two-dimensional array name a is a pointer to a pointer. It is also a pointer array name, which contains 3 elements, a[0]a[1], a[2]. Although the address stored by a is the address of the first element of the array, it does not point to a variable, but to a pointer. >>>More
Is it Xu Song's family-style love?
Lyrics: Crescent Moon Lakeside Movie Western Food Those Nights. >>>More
Hello. At the beginning, we were all friends, and then later, I fell in love with you, the ballad of songs, singing the sweetness you gave, and in the end, I lost you. >>>More
The United States wants to neutralize the country, but he wants to balance the world. >>>More