使用extern将数组与指针链接 [英] using extern to link array with pointer

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

问题描述

假设我有两个文件: file1.c-包含大小为10的int数组的全局定义,该数组名为"array [10]". file2.c-包含一个名为"extern int * array"的int指针,在这里我正尝试将此指针链接到array.

Suppose I have two files: file1.c- contains global definition of an int array of size 10 named "array[10]". file2.c- contains an int pointer named "extern int *array", here I am trying to link this pointer to array.

但是当我检查file1.c中数组的地址和file2.c中的指针值时,它们都是不同的.为什么会这样?

But when I check the address of array in file1.c and pointer value in file2.c, they are both different. Why it is happening?

推荐答案

这不起作用,在file2.c中,您需要

That doesn't work, in file2.c, you need

extern int array[];

因为数组和指针不是一回事.这两个声明必须具有兼容的类型,并且int*int[N]不兼容.

since arrays and pointers are not the same thing. Both declarations must have the compatible types, and int* is not compatible with int[N].

未指定实际发生的情况,程序的格式为extern int *array;,但是可能数组的前sizeof(int*)个字节被解释为地址.

What actually happens is not specified, the programme is ill-formed with extern int *array;, but probably, the first sizeof(int*) bytes of the array are interpreted as an address.

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

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