在C数组的名字是一个指针,它的第一要素,是不是? [英] In C. the name of an array is an pointer to it's first element, isn't it?

查看:183
本文介绍了在C数组的名字是一个指针,它的第一要素,是不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中code,我使用字符串作为数组,如果str的名称是一个指针str的第一元件,在这种情况下,字符串的第一个字符,这为s,大小STR,应该是8,但它给了6来代替。

In the following example code, I use string as array, if the name of str is an pointer to the first element of the str, in this case the first character of string, which is s, the size of str, should be 8, but it gives the 6 instead.

#include <stdio.h>

char str[] = "string";
char *ptr = "string";

int main(void){
    int first = sizeof(str);
    int second = sizeof(str[0]);
    int third = sizeof(ptr);
    int size_of_array = sizeof(str)/sizeof(str[0]);
    printf("the size is %d, %d, %d, %d", size_of_array, first, second, third);
    return 0;
}

这好像str是不是一个指针,str的大小不是PTR的大小?我很困惑约t

it seems like the str is not an pointer, the size of str is not the size of ptr? I'm confused about t

推荐答案

首先,你的问题的肉:,编译器将sizeof的严格的声明的规模而言在下面的code,假设一个指针的变量的拥有32位(4字节)地址:

First, the meat of your question: The compiler treats sizeof strictly in terms of declaration size. In the following code, assume a pointer variable holds a 32 bit (4 byte) address:

char str1[] = "abcd";
char *pstr = str1;

// str1 is declared to be a fixed array of 5 chars (4+nul)
size_t str1size = sizeof(str1); // str1size = 5;

// pstr is declared to be a 32 bit pointer to a memory address.
size_t pstrSize = sizeof(pstr) // size of a 32 bit pointer; 4.

要掌握你所看到的结果,阵列审核差异和指针变量,(其中有少于大多数人认为)。

To grasp the results you're seeing, review differences of arrays and pointer variables, (of which there are fewer than most think).

数组和指针的C语言中的核心分歧的变量的是

The core differences in C between arrays and pointer variables is


  • 指针变量的持有的地址;已声明数组的的地址。

  • 已声明数组,因此,不能有自己的地址的修改的(因为它们的的地址,他们没有的持有的地址就像一个指针变量)。

  • A pointer variable holds an address; a declared array is an address.
  • A declared array, therefore, cannot have their 'address' modified (because they are the address, they do not hold an address like a pointer variable).

其中第一个,我只是,如果你想真正环绕在C.比其他数组和指针你的头不能强调不够,它们的关系是乱伦的最好的。指针变量的在如数组的地址(或任何其他变量)。但不像阵列,他们也持有的地址作为自己的的。阵列的的他们非常地址。

The first of these I simply cannot stress enough if you want to really wrap your head around arrays and pointers in C. Other than that, their relationship is incestuous at best. Pointer variables are at addresses like arrays (or any other variable). But unlike arrays, they also hold an address as their value. Arrays are their very address.

第一与以下是最好的证明:

The first is best demonstrated with the following:

char str1[] = "abcd";
char *pstr = NULL;

pstr = str1;          // ok. pstr holds the address *at* str1.
str1[0] = *pstr;      // ok. copies 'a' on to 'a'.
pstr++;               // ok. increment the address held in pstr by one item (char) size.
str1[0] = *pstr;      // ok. copies 'b' over 'a'.
pstr = str1+1;        // ok. pstr holds the address *at* str1 + one item (char) size.
str1 = pstr;          // does not compile. str1's address cannot be changed.

请注意该指针的变量的本身的持有的地址,而固定阵列的的地址。因此,扭转过去的说法是完全正常的。

Note that the pointer variable itself holds an address, while a fixed array is an address. Thus the last statement reversed is perfectly fine.

pstr = str1;          // ok. put the str1 address in pstr, a pointer variable.

这篇关于在C数组的名字是一个指针,它的第一要素,是不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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