English 中文(简体)
浏览阵列和印刷,c
原标题:swapping array rows and printing, c
  • 时间:2011-09-25 15:35:41
  •  标签:
  • c

这是过去两小时对我ug笑的。 我试图把两行换成两个层面。 这不是一个问题,但我希望以能够点人的交换功能来做到这一点。 这里我迄今为止所做的事情:

#include <stdio.h>
#include <stdlib.h>

void swapRows(int **a, char **b)
{
    int *temp = *a;
    *a = *b; // WARNING #1
    *b = temp; // WARNING #1

}

void print(int a[][2],int rows,int cols)
{
    int i,j;
    for (i=0; i<rows; i++)
    {
        for (j=0; j<rows; j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("
");
    }
}

int main(void)
{
    int matrix[2][2] = {{1,2},{3,4}};
    print(matrix,2,2);
    swapRows(&matrix[0],&matrix[1]); //WARNING #2
    print(matrix,2,2);
    return EXIT_SUCCESS;
}

因此,我的问题是:

(1) 互换功能只在每一阵列中的第一个要素之间互换,这是有意义的。 我怎么去掉其他内容? 我是否赞成整整整段?

2) 声明其印刷时,只有一版<代码>a []2。 在我尝试撰写<代码>int **arr之前,我有分部分表决的过错。 为什么如此? 此外,我为什么必须具体说明阵列争论中的col号? 我有这方面的浏览和col,为什么编辑者迫使我这样做?

3)finally, using this version of print() I get a warning from the compiler passing argument 2 of ‘swapRows’ from incompatible pointer type (warning #2 in the code) and I get another warning (#1) : assignment from incompatible pointer type in swapRows(). Why is that? Thanks in advance!

最佳回答

The source of your confusion:

类型

int m1[2][2]; // a *single* block of memory of size 2*2*sizeof(int)
              //
              // this is a *real* 2d-array

<><>><>not>> 同>

int **m2;  // a pointer of size sizeof(int*) almost certainly the 
           // equal to sizeof(void*)
           //
           // This could be made to point at at least two logically 
           // distinct blocks of memory and *act* like a 2d-array

是的,m2 (pointer to a pointer) can/em> be de referenced using [][, but m1 (a) >>can not参引**>。

这意味着,你要求冲动输电是一种可怕的错误。 ......

  • matrix[0] is the first sub array (and int[2] containing {1,2}), taking the address of it is a null operation that gets the address of matrix[0][0], which is a spot in memory containing the integer representation of 1 (that is this is a pointer-to-int).
  • A similar argument applies to &matrix[1] it points a memory containing 2.

The you call swapRows which thinks that these arguments are pointers-to-pointers-to-int (which they are not, so your compiler throws a warning). It will run though because a pointer-to-int is the same size as a pointer-to-pointer-to-int (that isn t strictly guaranteed, but you ll usually get away with it). What it does when it runs is swap the point-to-int sized contents of a with the pointer-to-int sized contents of b. If it happens to be the case that sizeof(int) == sizeof(int*) (which is not uncommon), this will amount to swapping matix[0][0] with matrix[1][0].

这不是你想要的。

What can you do about it?

  • Declare matrix as a pointer to pointer and allocate the memory as in a ragged array, then use your existing swap
  • Declare matrix as an array of pointers and allocate memory for the rows, then use your existing swap
  • Keep the existing declaration of matrix and re-write the swap to swap every value.
  • Keep the existing declaration of matrix, but also provide an accessor array of pointers, use your existing swap on the accessor, and always use the accessor (the only possible advantage of this over the first two options is that everything resides on the stack).
  • Use the row-as-a-structure hack.

Row-as-a-structure

考虑

typedef struct {
   int r[2];
} row_t;
row_t m3[2];

<代码>m3是一系列结构,各为<编码>int。 查阅时间不多。 页: 1

row_t temp = m3[n];
m3[n] = m3[m];
m3[m] = temp;

a) 输电。

问题回答

射线和点照器是不同的,尽管C在许多情况下,它们的行为类似。 你们还意外地使用了果园,而不是第二个参数的 in子。 你的互换作用应宣布为:

void swapRows(int (*a)[2], int (*b)[2])

你们需要把(*a)[0]与(*b)[0]和(a) 等同(*b) 等同起来。

C正在将阵列变成一个点(有警告),但这种情况令人困惑。

It is just a limitation of C that arrays must have a size that is known at compile time.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签