我刚开始搞结构和指针。&array == array,结构怎么样?
这里是我的.h:
#ifndef struct_struct_h
#include <string.h>
#define struct_struct_h
#endif
int count=0;
typedef struct
{
int num;
double balance;
const char * name;
struct Account * acnt;
} Account;
Account* a = NULL;
Account* new_account(const char * n)
{
Account *a1 = malloc(sizeof(Account));
a1->num=++count;
a1->name = n;
return a1;
}
这里是我的main.c:
#include <stdio.h>
#include <string.h>
#include "struct.h"
int main(int argc, const char * argv[])
{
// insert code here...
Account* accounts[2];
for(int i=0; i<2; i++)
{
accounts[i] = (i==0 ? new_account("David") : new_account("Toto"));
}
printf("Accounts array address is %i\n",&accounts);
for(int i=0; i<2;i++)
{
printf("Account n°%i is owned by %s \n, its address is %i\n",accounts[i]->num,accounts[i]->name,&accounts[i]);
}
printf("There are %i accounts.\n",count);
return 0;
}
如果我的帐户替换&账户,我得到了相同的结果:@array,要么是&账户[0],没关系。
帐户阵列地址是1606416480
如果我更换&应收账款按*帐户,我得到这个:
帐户阵列地址是1063600
第二输出是:
账户N°1由大卫 拥有,其地址为1606416480
账户N°2由托托 拥有,其地址为1606416488
其实这些帐户指针的@载在账户中,这些@每个8B在内存中。
如果我更换&帐户[i]发表账户[I],然后通过*帐户[I]我得到:
账户N°1由大卫 拥有,其地址为1063600
帐户N°2由托托拥有,其地址被1063632
帐户N°1由大卫 拥有,其地址为3874
AC计数N°2由托托 拥有,其地址为3880
在第一种情况我有2个指针,并且在第二I具有2个*指针。
* STRUCT和STRUCT是不同的,为什么?
使用'%p'将参数强制转换为'void *'来打印地址。 –
如果一个数组是一个指针,它将被称为“指针”,而不是“数组”! &array是**不是**与'array'相同。即使** iff **数组_decays_大部分时间都是指向一个指针,它是一个不同的指针。而且你有更多的错误观念:你的“标题”是错误的。请查看头部必须包含的内容以及警卫的实际意图。 – Olaf
我对C很新,我读过“公共”结构应该在头部声明。 – Aleks