相关疑难解决方法(0)

为什么我不能将数组分配为&a =&b?

我有一个问题,分配如下数组:

int a[];
int b[] = {1,2,3};
&a = &b;
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用指针,但我想这样试试......

c

7
推荐指数
2
解决办法
2万
查看次数

合法的数组赋值.可能吗?

在阅读了关于K&R书中结构的章节后,我决定做一些测试来更好地理解它们,所以我写了这段代码:

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

struct test func(char *c);

struct test
{
    int i ;
    int j ;
    char x[20];
};

main(void)
{
    char c[20];
    struct  {int i ; int j ; char x[20];}  a = {5 , 7 , "someString"} , b; 
    c = func("Another string").x;
    printf("%s\n" , c);
}

struct test func(char *c)
{
    struct test temp;
    strcpy(temp.x , c);
    return temp;    
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:为什么c = func("Another string").x;工作(我知道这是非法的,但为什么它有效)?起初我用它写了strcpy()(因为这似乎是最合乎逻辑的事情)但我一直有这个错误:

structest.c: In function ‘main’:
structest.c:16:2: error: invalid …
Run Code Online (Sandbox Code Playgroud)

c arrays gcc c89 lvalue

7
推荐指数
1
解决办法
6031
查看次数

标签 统计

c ×2

arrays ×1

c89 ×1

gcc ×1

lvalue ×1