C内存阵列名称的位置

时间:2017-10-17 13:35:18

标签: c arrays pointers

我对C如何在内存中存储数组感到困惑。从下面的示例中,str1的地址等于其值。 Whit我将结果解释为:内存地址& str1包含地址本身的值。但事实并非如此,因为从其他例子中我看到str1的地址包含字符串的第一个值,在这种情况下是" H"。

所以我的问题可能是:存储了str1的地址在哪里?

#include <stdio.h>

int main(int argc, char const *argv[]) {
  char str1[] = "Hello World";
  printf("%d %d %c\n",&str1,str1,str1[0] );
  return 0;
}

1 个答案:

答案 0 :(得分:1)

首先,您必须使用%p格式说明符来打印地址(指针)。

话虽如此,引用C11,章节§6.3.2.1

  

除非它是sizeof运算符,_Alignof运算符或者&运算符的操作数。   一元 printf("%p", str1); printf("%p", &str1[0]); 运算符,或者是用于初始化数组的字符串文字,一个包含的表达式   类型''数组''转换为类型为''指向类型'指针的表达式   到数组对象的初始元素,而不是左值。

所以,基本上,

&str1

是一样的。

现在,来到char [<array size>]的部分,嗯,类型是不同的。它的类型为export default class App extends Component { componentWillMount() { fetch('https://httpbin.org/get') .then(function(response) { this.setState({ infoStatus: 'loaded' }); return response.json(); }) .then(function(data) { this.setState({ city: data.name, country: data.sys.country, temperature: data.main.temp, humidity: data.main.humidity, wind: data.wind.speed }); }) .catch(function () { main.setState({ infoStatus: 'error' }); }); } render() {...} }

相关问题