Converting C code to C#, cannot understand a certain line in C

So, I have some C code for a bitonic sort, and I am trying to convert that code into C#. One of the lines in the C code is confusing me. I have never worked with C before. void merge_up(int *arr, int n) { int step=n/2,i,j,k,temp; while (step > 0) { for (i=0; i < n; i+=step*2) { for (j=i,k=0;k < step;j++,k++) { if (arr[j] > arr[j+step]) { // swap temp = arr[j]; arr[j]=arr[j+step]; arr[j+step]=temp; } } } step /= 2; } } void merge_down(int *arr, int n) { int step=n/2,i,j,k,temp; while (step > 0) { for (i=0; i < n; i+=step*2) { for (j=i,k=0;k < step;j++,k++) { if (arr[j] < arr[j+step]) { // swap temp = arr[j]; arr[j]=arr[j+step]; arr[j+step]=temp; } } } step /= 2; } } void printArray(int *arr, int n) { int i; printf("[%d",arr[0]); for (i=1; i < n;i++) { printf(",%d",arr[i]); } printf("]\n"); } int main(int argc, char **argv) { int n, *arr, i,s; FILE *fp = fopen(argv[1],"r"); if (fp == NULL) { fprintf(stderr,"file not found\n"); exit(1); } // first line gives number of numbers to be sorted fscanf(fp,"%d",&n); // allocate space and read all the numbers arr = (int *)malloc(n*sizeof(int)); for (i=0; i < n; i++) { fscanf(fp,"%d",(arr+i)); } // print array before printArray(arr,n); // do merges for (s=2; s <= n; s*=2) { for (i=0; i < n;) { merge_up((arr+i),s); merge_down((arr+i+s),s); //Having trouble with this line here. i += s*2; } } printArray(arr,n); } What is it doing when it calls merge_down((arr+i+s), s); Specifically, the arr+i+s. arr is the array, but what is +i+s doing? I would really appreciate some help. -EDIT: I should add what ive done in C# for that part. This is what ive got: //Do merges for (int s = 2; 2 <= n; s = s * 2) { for(int i = 0; i < n;){ mergeUp(arr, s); mergeDown(arr, s); i += s * 2; } }
I'm sure this is a duplicate. arr is a pointer to a location in memory, and in C, arrays are stored contiguously. arr+i+s is also a pointer to a location, and the + operator magically computes the location based on the type of the array. It is essentially a subarray starting at the (i+s)th element.

以上就是Converting C code to C#, cannot understand a certain line in C的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏