Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the problems of passing parameters with two-dimensional group names?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the two-dimensional group name to do parameter transfer problems, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Segmention Error is easy to occur when passing a two-dimensional group name as a parameter. This is due to the problem of incorrectly addressing elements in a two-dimensional array, and the correct approach is as follows:

1. Use a pointer variable to an one-dimensional array, as shown in the following example:

# include # include # define N 4void testArr (int (* a) [N], int m) {for (int I = 0; I < m; + + I) for (int j = 0; j < N; + + j) {printf ("a [% d] [% d] =% d\ n", I, j, a [I] [j]) }} int main () {int a [2] [N] = {{1,2,3,4}, {5,6,7,8}; testArr (a, 2);}

Int (* a) [N] represents a pointer variable to an one-dimensional array, that is, the object a points to is an array of four integer elements. Note that () can not be less, if defined as:

Int * a [N] means that there is an one-dimensional array a [N], in which all elements are of type (int *).

Here, you can access elements in a two-dimensional array in a subfunction using a [I] [j] or * (* (axii) + j)

two。 Pass the two dimensions of the two-dimensional array in the form of variables

As follows:

# include # include # define N 4void testArray (int* * a, int m, int n) {for (int I = 0; I < m; + + I) for (int j = 0; j < n; + + j) {printf ("a [% d] [% d] =% d\ n", I, j, * (int*) a + I * n + j) }} int main () {int a [2] [N] = {{1,2,3,4}, {5,6,7,8}}; testArray ((int * *) a, 2, N);}

At this time, the array elements cannot be accessed in the form of a [I] [j] in the subfunction, because the array elements are stored sequentially, the address is continuous, and the specified elements cannot be accessed sequentially when using a [I] [j] to access the array elements. so we can only calculate the elements we want to access.

On the two-dimensional group name to do the parameter transfer problem has been shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report