传递数组指针的用C [英] Passing pointers of arrays in C

查看:123
本文介绍了传递数组指针的用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些code,看起来像这样:

So I have some code that looks like this:

int a[10];
a = arrayGen(a,9);

和arrayGen功能如下:

and the arrayGen function looks like this:

int* arrayGen(int arrAddr[], int maxNum)
{
    int counter=0;
    while(arrAddr[counter] != '\0') {
        arrAddr[counter] = gen(maxNum);
        counter++;
    }
    return arrAddr;
}

眼下compilier告诉我:警告:传递的arrayGen'参数1使得整数指针,未作类型转换

Right now the compilier tells me "warning: passing argument 1 of ‘arrayGen’ makes integer from pointer without a cast"

我的想法是,我通过'A',指向一个[0],则因为阵列已经创建我可以只填写值了[N],直到我一个[N] =='\\ 0 。我觉得我的错误是,arrayGen写入采取一个数组,而不是一个指针之一。如果这是真的,我不知道如何着手,做我值写入地址直到一个地址的内容是'\\ 0'?

My thinking is that I pass 'a', a pointer to a[0], then since the array is already created I can just fill in values for a[n] until I a[n] == '\0'. I think my error is that arrayGen is written to take in an array, not a pointer to one. If that's true I'm not sure how to proceed, do I write values to addresses until the contents of one address is '\0'?

推荐答案

这里的基本神奇的是这个身份在C:

The basic magic here is this identity in C:

*(a+i) == a[i]

好吧,现在我要让这是可读的英语。

Okay, now I'll make this be readable English.

下面的问题:一个数组名是不是左值;它不能被分配​​到。所以,你必须与行

Here's the issue: An array name isn't an lvalue; it can't be assigned to. So the line you have with

a = arrayGen(...)

就是问题所在。见这个例子:

is the problem. See this example:

int main() {
    int a[10];

    a = arrayGen(a,9);

    return 0;
}

这给编译错误:

gcc -o foo foo.c
foo.c: In function 'main':
foo.c:21: error: incompatible types in assignment

Compilation exited abnormally with code 1 at Sun Feb  1 20:05:37

您需要有一个指针,它的的左值,到分配的结果。

You need to have a pointer, which is an lvalue, to which to assign the results.

这code,例如:

int main() {
    int a[10];
    int * ip;

    /* a = arrayGen(a,9);  */
    ip = a ; /* or &a[0] */
    ip = arrayGen(ip,9);

    return 0;
}

编译罚款:

gcc -o foo foo.c

Compilation finished at Sun Feb  1 20:09:28

请注意,由于在顶部的标识,你可以把知识产权作为一个数组,如果你喜欢,因为在这种code:

Note that because of the identity at top, you can treat ip as an array if you like, as in this code:

int main() {
    int a[10];
    int * ip;
    int ix ;

    /* a = arrayGen(a,9);  */
    ip = a ; /* or &a[0] */
    ip = arrayGen(ip,9);

    for(ix=0; ix < 9; ix++)
        ip[ix] = 42 ;

    return 0;
}



只是为了完整性这里是我的完整的例子:

Just for completeness here's my full example:

int gen(int max){
    return 42;
}

int* arrayGen(int arrAddr[], int maxNum)
{
    int counter=0;
    while(arrAddr[counter] != '\0') {
        arrAddr[counter] = gen(maxNum);
        counter++;
    }
    return arrAddr;
}

int main() {
    int a[10];
    int * ip;
    int ix ;

    /* a = arrayGen(a,9);  */
    ip = a ; /* or &a[0] */
    ip = arrayGen(ip,9);

    for(ix=0; ix < 9; ix++)
        ip[ix] = 42 ;

    return 0;
}

这篇关于传递数组指针的用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆