本文介绍了在C数组,指针和字符串之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是心里有一个问题,困扰我。

Just have a question in mind that troubles me.

我知道指针阵列在C不同,因为指针存储一个地址,而阵列商店真实价值

I know pointers and arrays are different in C because pointers store an address while arrays store 'real' values.

但是我却越来越困惑,当谈到字符串

But I'm getting confused when it comes to string.

char *string = "String";

我读到这条线做几件事情:

I read that this line does several things :

字符数组是由编译器创建,其值为字符串

An array of chars is created by the compiler and it has the value String.

然后,这个数组被视为指针和程序分配到指针字符串,它指向第一个元素由编译器创建的数组。

Then, this array is considered as a pointer and the program assigns to the pointer string a pointer which points to the first element of the array created by the compiler.

这意味着,阵列被视为指针

那么,这个结论真的还是假的?为什么?

So, is this conclusion true or false and why ?

如果假的,有什么话指针阵列之间的区别是什么?
谢谢你。

If false, what are then the differences between pointers and arrays ?Thanks.

推荐答案

一个的指针的包含对象的地址(或者是一个空指针不指向任何对象)。指针具有特定的类型,指示它可以指向对象的类型。

A pointer contains the address of an object (or is a null pointer that doesn't point to any object). A pointer has a specific type that indicates the type of object it can point to.

这是阵列的元素是一个连续有序序列;每个元素是一个对象,并且阵列的所有元素都是同一类型的

An array is a contiguous ordered sequence of elements; each element is an object, and all the elements of an array are of the same type.

一个的字符串的定义为通过终止字符的连续序列,并包括第一个空字符。 C有没有字符串类型。字符串是一个数据的布局,而不是数据类型。

A string is defined as "a contiguous sequence of characters terminated by and including the first null character". C has no string type. A string is a data layout, not a data type.

数组和指针之间的关系可能会造成混淆。我所知道的最好的解释是由第6节给出的。要记住的最重要的事情是的数组不是指针

The relationship between arrays and pointers can be confusing. The best explanation I know of is given by section 6 of the comp.lang.c FAQ. The most important thing to remember is that arrays are not pointers.

数组在某种意义上是二等公民,在C和C ++。他们不能被指派,作为函数的参数传递,或相等比较。 code,通常操纵数组并因此使用指针数组的各个元素,用一些明确的机制来指定数组有多长。

Arrays are in a sense "second-class citizens" in C and C++. They cannot be assigned, passed as function arguments, or compared for equality. Code that manipulates arrays usually does so using pointers to the individual elements of the arrays, with some explicit mechanism to specify how long the array is.

混乱的一个重要原因是,数组类型(如数组对象的名称)的前pression是的隐式转换的在大多数情况下指针值。转换后的指针指向数组的初始(零)元素。如果数组是这种转换不会发生或者:

A major source of confusion is the fact that an expression of array type (such as the name of an array object) is implicitly converted to a pointer value in most contexts. The converted pointer points to the initial (zeroth) element of the array. This conversion does not happen if the array is either:


  • 的操作数的sizeof 的sizeof array_object 产生的数组的大小,而不是一个指针的大小)

  • 的一元操作数&安培; &放大器; array_object 产生数组对象的地址作为一个整体) ;或

  • 在用于初始化数组对象的初始化字符串文字。

  • The operand of sizeof (sizeof array_object yields the size of the array, not the size of a pointer);
  • The operand of unary & (&array_object yields the address of the array object as a whole); or
  • A string literal in an initializer used to initialize an array object.

的char *字符串=字符串;

char *string = "String";

为了避免混淆,我要使你的榜样一些变化:

To avoid confusion, I'm going to make a few changes in your example:

const char *ptr = "hello";

的字符串你好创建类型的匿名对象的char [6] (C语言)或为const char [6] (在C ++),包含字符 {'H','E','L','L',' O','\\ 0'}

The string literal "hello" creates an anonymous object of type char[6] (in C) or const char[6] (in C++), containing the characters { 'h', 'e', 'l', 'l', 'o', '\0' }.

这前pression的评价,在这种情况下,产生一个指向数组的初始字符。这是一个指针的的;没有隐式创建指针对象。该指针值用于初始化指针对象 PTR

Evaluation of that expression, in this context, yields a pointer to the initial character of that array. This is a pointer value; there is no implicitly created pointer object. That pointer value is used to initialize the pointer object ptr.

目前没有时间是数组的指针作为处理。数组前pression是的转换的为指针类型。

At no time is an array "treated as" a pointer. An array expression is converted to a pointer type.

混乱的另一个来源是,似乎是数组类型是指针类型实际上该函数的参数;该类型的调整的在编译时。例如,这

Another source of confusion is that function parameters that appear to be of array type are actually of pointer type; the type is adjusted at compile time. For example, this:

void func(char param[10]);

真正含义是:

void func(char *param);

10 被忽略。所以,你可以这样写:

The 10 is silently ignored. So you can write something like this:

void print_string(char s[]) {
    printf("The string is \"%s\"\n", s);
}
// ...
print_string("hello");

此的看起来的像只运用阵列,但实际上阵列你好转换为指针,该指针是什么是传递在 print_string 功能。

This looks like just manipulating arrays, but in fact the array "hello" is converted to a pointer, and that pointer is what's passed to the print_string function.

这篇关于在C数组,指针和字符串之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 11:52