gpt4 book ai didi

c - C中的二维指针算法

转载 作者:太空宇宙 更新时间:2023-11-04 01:40:22 25 4
gpt4 key购买 nike

我试图用 C 语言交换二维数组中的 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 的建议将不胜感激。

最佳答案

  1. 最后一个元素的索引是count-1,而不是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. 我不知道什么是fwnodes,也许你的意思是h

关于c - C中的二维指针算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6402979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com