C 中的 2D 指针算术

发布于 2024-11-16 04:30:54 字数 1515 浏览 8 评论 0原文

我试图用 C 语言交换 2D 数组中的 2 个元素,但没有成功。

感谢到目前为止的答案,但我已经编辑了这段代码,以使我在做什么更加清楚。

typedef struct smystruct { /* stuff... */ } mystruct;

void nswap( mystruct ** a, mystruct ** b )
{
  mystruct * tmp = *a;
  *a = *b;
  *b = tmp;
}

void nqsort( mystruct ** h, int m, int n )
{
  double key = 0.0;
  int i = 0, j = 0, k = 0;

  if( m < n ) {
    // choose the pivot point...
    k = (m + n) / 2;

    nswap( &h[ n ], &h[ k ] );

    key = (*h+m)->prob;

    i = m + 1;
    j = n;

    while ( i <= j ) {
      while ( (i <= n) && (*h+i)->prob <= key )
        i++;
      while ( (j >= m) && (*h+j)->prob > key )
        j--;
      if ( i < j ) {
        nswap( &h[i], &h[j] );
      }
    }

    // swap two elements
    nswap( &h[m], &h[j] );

    // recursively sort the lesser list
    nqsort( h, m, j-1 );
    nqsort( h, j+1, n );
  }
}

int main()
{
  mystruct * p = NULL;

  // get the number of nodes (m)...
  fscanf( in, "%d", &m );

  // allocate memory for the node and connectivity matrix arrays...
  p = (mystruct*)malloc( sizeof( mystruct ) * m );

  // read in the location and associated probabilities!...
  for ( ; loop < m ; ++loop ) {
    mystruct * tmpnode = p + loop;
    tmpnode->str = (char*)malloc( sizeof( char ) * 1024 );
    fscanf( in, "%s %lf", (char *)tmpnode->str, &tmpnode->prob );
  }

  nqsort( &p, 0, m );
}

不用说,这是行不通的。我搜索过示例,但似乎没有任何效果。为 n00b 提供建议将不胜感激。

I am attempting to swap 2 elements in a 2D array in C, with no luck.

Thanks for the answers so far, but I have edited this code to make things clearer about what I am doing.

typedef struct smystruct { /* stuff... */ } mystruct;

void nswap( mystruct ** a, mystruct ** b )
{
  mystruct * tmp = *a;
  *a = *b;
  *b = tmp;
}

void nqsort( mystruct ** h, int m, int n )
{
  double key = 0.0;
  int i = 0, j = 0, k = 0;

  if( m < n ) {
    // choose the pivot point...
    k = (m + n) / 2;

    nswap( &h[ n ], &h[ k ] );

    key = (*h+m)->prob;

    i = m + 1;
    j = n;

    while ( i <= j ) {
      while ( (i <= n) && (*h+i)->prob <= key )
        i++;
      while ( (j >= m) && (*h+j)->prob > key )
        j--;
      if ( i < j ) {
        nswap( &h[i], &h[j] );
      }
    }

    // swap two elements
    nswap( &h[m], &h[j] );

    // recursively sort the lesser list
    nqsort( h, m, j-1 );
    nqsort( h, j+1, n );
  }
}

int main()
{
  mystruct * p = NULL;

  // get the number of nodes (m)...
  fscanf( in, "%d", &m );

  // allocate memory for the node and connectivity matrix arrays...
  p = (mystruct*)malloc( sizeof( mystruct ) * m );

  // read in the location and associated probabilities!...
  for ( ; loop < m ; ++loop ) {
    mystruct * tmpnode = p + loop;
    tmpnode->str = (char*)malloc( sizeof( char ) * 1024 );
    fscanf( in, "%s %lf", (char *)tmpnode->str, &tmpnode->prob );
  }

  nqsort( &p, 0, m );
}

Needless to say this does not work. I have searched for examples and nothing seems to work. Advise for the n00b would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ペ泪落弦音 2024-11-23 04:30:54
  1. 最后一个元素的索引为 count-1,而不是 count

    h[0] h[1] h[2] h[3] h[4] h[5] h[6] h[7] h[8] h[9]
    -------------------------------------------------- --------
                         共 10 个元素
    
  2. 我不知道什么是fwnodes,也许你的意思是h

  1. The last element has index count-1, not count.

    h[0]  h[1]  h[2]  h[3]  h[4]  h[5]  h[6]  h[7]  h[8]  h[9]
    ----------------------------------------------------------
                         total 10 elements
    
  2. I don't know what is fwnodes, perhaps you mean h.

蒗幽 2024-11-23 04:30:54

那里有很多东西是错误的。

1/ 您的 2D 数组分配不当(或代码丢失)。
2/ 进行此类 2D 分配的正确方法是使用 Iliffe 指针(如 C/C++ 中的数字食谱中所建议)。

mystruct** alloc_array( int h, int w )
{
  int i;
  mystruct** m = malloc(h*sizeof(mystruct*));
  m[0] = malloc(h*w*sizeof(mystruct));
  for(i=1;i<h;i++) m[i] = m[i-1]+w;
  return m;
}

void release_array(mystruct** m)
{
  free( m[0] );
  free( m);
}

这种分配方式为您带来了连续的内存块(更容易处理,对缓存更友好,不需要进行一些索引计算)和 [][] 访问。

然后您的交换函数将变为:

void swap( mystruct* a, mystruct* b )
{
  mystruct tmp = *a
  *a = *b;
  *b = tmp;
}

并可以像这样调用:

swap( &some_tab[i][j], &some_other_tab[u][v] );

在完整的示例中:

int main()
{
  mystruct** my_array = alloc_array(3,4); /* 3x4 mystruct array */

  /* fill the array */

  /* Swap some */

  swap( &my_array[2][1], &my_array[0][3] );


  release_array(my_array);
}

Multiple stuff is wrong there.

1/ Your 2D array is badly allocated (or code is missing).
2/ A proper way to do such 2D allocation is to use Iliffe pointer (as advised in Numrical Recipes in C/C++).

mystruct** alloc_array( int h, int w )
{
  int i;
  mystruct** m = malloc(h*sizeof(mystruct*));
  m[0] = malloc(h*w*sizeof(mystruct));
  for(i=1;i<h;i++) m[i] = m[i-1]+w;
  return m;
}

void release_array(mystruct** m)
{
  free( m[0] );
  free( m);
}

This way of allocating brings you both a contiguous block of memory (which is easier to handle, is more cache friendly, dont require to do some index computation) and a [][] access.

Your swap function then become :

void swap( mystruct* a, mystruct* b )
{
  mystruct tmp = *a
  *a = *b;
  *b = tmp;
}

and can be called like :

swap( &some_tab[i][j], &some_other_tab[u][v] );

In a full example :

int main()
{
  mystruct** my_array = alloc_array(3,4); /* 3x4 mystruct array */

  /* fill the array */

  /* Swap some */

  swap( &my_array[2][1], &my_array[0][3] );


  release_array(my_array);
}
朕就是辣么酷 2024-11-23 04:30:54

您正在将指向数组的指针传递给您的函数。这意味着 h 只有一个元素。

swap( &p, 10 );

应该是:

swap( p, 10 );

这意味着您需要更改函数以接受 mystruct 数组,或将 p 更改为指向 mystruct 的指针数组。

并且,正如 KennyTM 所建议的,最后一个元素的索引为 9 而不是 10。

You are passing pointer to array to your function. That means h has only one element.

swap( &p, 10 );

Should be:

swap( p, 10 );

That means you need to change your function to accept arrays of mystruct or change p to array of pointers to mystruct.

And, as KennyTM suggested the last element has index 9 not 10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文