如何通过C中的引用引用二维数组字符

时间:2014-03-13 17:14:11

标签: c arrays reference char

我正在尝试在 C 中创建2D array chars,然后将pointer传递给另一个function,这将分配给chararrayserrors每行的第一个点...我不断变化rows[0][0],这里有一些代码。此外,所有分配必须通过指针算法完成,而不是使用char(*rows)[size] = malloc(sizeof(*rows) * size); otherFunction(seed, size, &rows); } void otherFunction(int seed, int size, char (**rows)[]) { int count = 0; while (count < (size*size)) { (&&rows + count) = 'X'; count += size; }

char test[size][size];
makeTest(seed, size, &test);
displayTest(size, &test);
}

void makeTest(int seed, int size, char (*test)[size][size]) {
    int count = 0;
        while (count < (size*size)) {
    *(&test + count) = 'X';
        count += size;
    }
}

void displaytest(int size, char(*test)[size][size]) {
    for (int row = 0; row < (size*size); row += size) {
            for (int count = 0; count < size; count++) {
                printf("%c", *(test + row + count));
            }
             printf("\n");
         }
}

我知道这是错的,但我不确定如何纠正

修改 使用你的建议我修改了代码,但它仍然是不对的,我已经尝试将其打印出来,我希望它按行顺序打印

char test[size][size];
generateTest(seed, size, *test);
displayTest(size, *test);
}

void generateTest(int seed, int size, char *test) {
for (int row = 1; row < size - 1; row += 1) {
    for (int column = 0; column < size; column++) {
        *(test + (row * size) + column) = 'X';
        }
    }
}

在makeTest中指定X得到的  警告:赋值从整数中生成指针而不进行强制转换 在显示测试中我打印我得到的字符 警告:格式â%câ期望类型âintâ,但参数2的类型为âchar(*)[(long unsigned int)(size)]â

最终修改 在我得到一个足够帮助我的答案之前,我设法找到了一个在我自己有所帮助的答案,我将在这里发布以防万一它也可以帮助其他人

{{1}}

虽然边界是screwey,但是b / c某些行被其他循环覆盖,但这大致是我做的方式

5 个答案:

答案 0 :(得分:0)

为什么不使用指针指针而不是在数组中混合?

char **rows = malloc(sizeof(char *) * size);
for (int i = 0; i < size; ++)
    rows[i] = malloc(sizeof(char) * size);

/* Now `rows` can be used as a "2d array", or a matrix of size `size` x `size` */

otherFunction(seed, size, rows);

...

void otherFunction(int seed, int size, char **rows)
{
    for (int i = 0; i < size; ++i)
    {
        for (int j = 0; j < size; ++j)
        {
            rows[i][j] = 'X';
        }
    }
}

答案 1 :(得分:0)

在我不理解某些事情的情况下,我总是帮助我制作示例代码,因此我将首先发布一个我将要做的事情的示例。我会把它作为练习让你修改它以做你需要的任何事情。

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

#define STRLEN_PLUS_NULL( s ) ( strlen( s ) + 1 )
#define AMOUNT 10
#define LENGTH 80

char ** CreateArrayOfStrings( int amount, int length )
{
    int i = 0;
    char ** stringArray = malloc( sizeof( char * ) * amount );

    if ( !stringArray )
    {
        perror( "Malloc" );
        return NULL;
    }

    for ( ; i < amount; ++i )
    {
        if ( !( stringArray[i] = malloc( length ) ) )
        {
            perror( "Malloc" );

            for ( --i; i >= 0; --i )
                free( stringArray[i] );
            free( stringArray );

            return NULL;
        }
    }

    return stringArray;
}

void FreeArrayOfStrings( char ** strings, int amount )
{
    int i = 0;

    for ( ; i < amount; ++i )
        free( strings[i] );

    return;
}

int main()
{
    int i = 0;
    char ** myStrings = CreateArrayOfStrings( AMOUNT, LENGTH );

    if ( !myStrings ) return 1;

    for ( ; i < AMOUNT; ++i )
        memcpy( myStrings[i], "X", STRLEN_PLUS_NULL( "X" ) );
    for ( i = 0; i < AMOUNT; ++i )
        printf( "%s\n", myStrings[i] );

    FreeArrayOfStrings( myStrings, AMOUNT );
    free( myStrings );
    return 0;
}

让我们通过我的CreateArrayOfStrings()函数,首先我在内存中创建一组字符指针,其数量由amount参数确定。然后我测试它以查看它是否已分配或malloc()是否返回错误(始终检查malloc()来电时的错误)。之后,我遍历我新创建的函数指针数组,并将每个函数指针设置为指向内存中的长度字节。如果在分配该内存时出错,我会反向遍历我的数组并返回null。

在我的main函数中,我将名为myStrings的char **变量设置为CreateArrayOfStrings()的返回值。我测试它以确保没有错误。然后再次循环遍历数组,将先前分配的每个字符指针的内存设置为我在memcpy()中指定的字符串文字。之后我打印结果并清理我的资源。

同样,我没有在我的代码中使用指针表示法,但这将是一个微不足道的变化。您应该首先弄清楚如何使用数组表示法,一旦您知道,您可以尝试转换它。

答案 2 :(得分:0)

尝试将char *传递到2D数组的开头(并相应地更改otherFunction())。

因此,请将呼叫更改为:

otherFunction(seed, size, rows[0]);

otherFunction()

void otherFunction(int seed, int size, char *rows) {
    int count = 0;
    while (count < (size*size)) {
        *(rows + count) = 'X';
        count += size;
    }
}

使用C99,您还可以将2D数组作为2D数组(或指向1D数组的指针)传递,使用size来获取尺寸。在这种情况下,呼叫可以是:

otherFunction(seed, size, rows);

... otherFunction()可以简化为:

void otherFunction(int seed, int size, char (*rows)[size]) {
    int count;
    for (count = 0; count < size; count++)
        rows[i][0] = 'X';
}

虽然它使用[]运算符,但如果你想使用指针运算的原因是允许不同的数组维度,那么在这种情况下它应该仍然没问题。

答案 3 :(得分:0)

样品

#include <stdio.h>
#include <string.h>

void makeTest(int seed, int size, char (*test)[size][size]) {
    int count = 0;
    while (count < (size*size)) {
        *((char*)test + count) = 'X';
        count += size;
    }
}

void displayTest(int size, char(*test)[size][size]) {
    for (int row = 0; row < size*size; row+=size) {
        for (int count = 0; count < size; count++) {
            printf("%c", *((char*)test + row +count));
        }
        printf("\n");
    }
}

int main(){
    int size = 5;
    int seed = 1;
    char test[size][size];
    memset(test, '?', sizeof(test));
    makeTest(seed, size, &test);
    displayTest(size, &test);
}

答案 4 :(得分:-1)

没必要恐慌 您可以通过引用简单地传递参数:

void main()
{char ch[3][3];
 .
 .
 func(&ch);
 .
}

void func(char *ch[3][3])
{.
 .
 //use it here as you want
 .
}