当声明char Str [16]时,Str是一个指针吗? [英] When declare char Str[16] then Str is a pointer?

查看:111
本文介绍了当声明char Str [16]时,Str是一个指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
这是我的问题,需要您的帮助:当我声明:

Hello,
This is my problem to need you to help me: When I declare:

char *str;


如果我声明以下内容,则str是肯定的指针:


then str is sure a pointer, if I declare the following:

char str[16];


目前str仍然是指针吗?还是变量?请帮我.感谢您的进步.


At the moment str is still a pointer? Or a variable? Plz help me. Thank for advances.

推荐答案

char *str;声明了一个指针.该指针没有值,但是您可以通过将其分配给另一个变量来更改它.此表示法通常用于在堆栈上分配的动态字符串,或在解析它时跟踪另一个字符串中的当前位置.
指针值(4个字节(在64位平台上为8个字节))存储在堆栈中,而引用的内存可以存储在堆中(newmalloc,...)

char *str; declares a pointer. this pointer has no value, but you can change that by assigning it to another variable. This notation is usually used for dynamic strings allocated on the stack or keeping track of the current position in another string when parsing it.
the pointer value (4 bytes (8 on a 64bit platform)) is stored in the stack, while the memory referenced may be stored in the heap (new, malloc, ...)

char str[16];

在堆栈上分配空间(16 * sizeof(char)= 16字节)(在存储函数调用和变量的同一区域中)
就编译器而言,这也是一个指针,但这指向字符串中的第一个字符.编译后,将不会为其存储任何指针值.

这将再次引用与堆栈上相同的字符串.更改1个字符串将同时更改两个字符串.
通常用于在字符串的字符中进行提示以找到我们感兴趣的1,例如一个空格字符.

allocates space on the stack (16 * sizeof(char) = 16 bytes) (in the same area where function calls and variables are stored)
This is also a pointer as far as the compiler is concerned, but this points to the first character in the string. Once compiled, there is no pointer value stored for it.

This makes another reference to the same string as the one on the stack. Changing 1 string will change both strings.
This is usually used for itterating through the characters of the string to find 1 that we are interested in, say a space char.

char strStack[12] = "hello world"; //allocate 12 chars on the stack
char *strStackPtr = strStack;



这将分配一个新字符串以复制现有字符串的内容.因为我们不知道另一个字符串的长度(只是假装我们不知道),所以我们需要动态分配它,以便我们可以修改一个字符串而不影响另一个



This allocates a new string to copy the contents of an existing one. Because we dont know the length of the other string (just pretend we dont) we need to allocate it dynamically so that we can modify 1 string without affecting the other

char strStack[12] = "hello world"; //allocate 12 chars on the stack
size_t nlen = strlen(strStack);
char *strHeap = new char[nlen];
strcpy(strHeap, strStack);
//do something with strHeap
delete []strHeap; //anything allocated on the heap must be free''d



堆栈上的空间有限,因此任何长字符串都应使用newmalloc分配在堆上.时期.对于小字符串,您是安全的(通常最好使用堆栈).在堆栈上分配字符串要快得多(也容易得多),并且在超出范围时会释放自身.

对于经过硬编码且永不更改的字符串,最好只使用



There is limited space on the stack, so any long strings should be allocated on the heap with new or malloc. period. For small strings, you are safe (and often better to use the stack). It is much much quicker (and easier) to allocate a string on the stack, and it frees itself when it goes out of scope.

For strings that are hard coded and never going to change, you are best to just use

char *strSeg = "hello world";


这将在.exe文件中分配字符串,而strSeg是指向.exe在加载到内存中的字符串位置的指针


This will allocate the string in the .exe file, and strSeg is a pointer to the location of the string in the .exe loaded into memory


char str[16];



不是指针,而是由16个字符组成的数组.数组是内存中连续事物的块,指针是包含其他对象地址的变量.

您可能会感到困惑的是,数组的名称与数组的第一个元素的地址同义.因此,您可以使用数组的名称进行初始化或分配给指针.

因此,如果您有指针,则可以在其上使用数组样式索引,但是如果您有数组名称,则不能在其上进行指针样式分配.如果您执行以下操作:



isn''t a pointer, it''s an array of 16 characters. A array is a block of contiguous things in memory, a pointer is a variable that contains the address of something else.

Where you might be getting confused is that the name of an array is synonymous with the address of the first element of the array. So you can use the name of an array to initialise or assign to pointers.

So if you have a pointer you can use array style indexing on it, but if you have an array name you CAN''T do pointer style assignment on it. If you do something like:

char str[16];
char *p = str;



str和p指向内存中的相同位置.然后,您可以执行以下操作:



str and p refer to the same location in memory. You can then do something like:

p++;



p现在将指向str[ 1 ]的存储位置.但是,如果您尝试:



and p will now point to the memory location of str[ 1 ]. However if you try:

str++;



编译器会告诉您散步,因为您无法修改str表示的值,它是一个常量(如果您尝试阅读它,至少在C90中,它是标准语言中的一个右值,不确定C99).

因此,数组名称和指针之间的区别是:

-指针包含地址
-数组名称是地址
-执行sizeof array_name可为您提供数组中的字节数
-执行sizeof指针将返回在系统上保存地址所需的字节数

现在还有另一个混乱的根源.当您将数组传递给函数时,编译器将获取数组中第一个元素的地址,并将其转换为指针.因此,当您看到类似以下的函数原型时:

无效测试(char b [20]);

您可以这样阅读:

无效测试(char * b);

并且如果您在函数中对b执行sizeof运算,它将根据编译器的不同而返回4或8(在非常老的系统或嵌入式系统上可能为2).

所以我们可以添加一些规则:

-指针包含地址
-数组名称是地址
-执行sizeof array_name可为您提供数组中的字节数
-执行sizeof指针将返回在系统上保存地址所需的字节数
-读取传递给函数的所有数组名称,作为该函数范围内的指针

干杯,

Ash



the compiler will tell you take a walk as you can''t modify the value str represents, it''s a constant (it''s an rvalue in standard speak if you ever try reading it, at least in C90, not sure about C99).

So the differences between array names and pointers are:

- pointers contain addresses
- array names are addresses
- doing a sizeof array_name gives you the number of bytes in the array
- doing a sizeof a pointer returns the number of bytes it takes to hold an address on your system

Now there''s another source of confusion. When you pass an array to a function the compiler takes the address of the first element in the array and converts it to a pointer. So when you see a function prototype like:

void test( char b[ 20 ] );

you can read it like:

void test( char *b );

and if you do a sizeof on b within the function it''ll come back as 4 or 8 (perhaps 2 on very old or embedded systems) depending on you compiler.

So we can add to the rules a bit:

- pointers contain addresses
- array names are addresses
- doing a sizeof array_name gives you the number of bytes in the array
- doing a sizeof a pointer returns the number of bytes it takes to hold an address on your system
- read all array names passed to functions as pointers within the scope of that function

Cheers,

Ash


是否正确的问题是:指针还是指针-仍然是变量或函数参数.在两种情况下,str都是指针.

您的第一行和最后一行之间的区别:如果是第一行,则指针尚未初始化:指向哪个字符?在第二行中,为16个字符分配了一个内存.如果这是本地声明,则在执行功能时将其分配在本地堆栈上(如果不是)-在进程静态内存中.指针已经初始化,并指向16数组的第一个字符.


好吧,由于此页面上的激烈争论,我承认我需要详细说明数组与指针"这一主题.

当然,我知道数组和指针并不完全相同.在以上段落中,我描述了内存布局的区别:使用数组时,始终在初始化(堆栈或静态)期间获取内存.还有更多.考虑示例:

The question if not correct: a pointer or not a pointer -- still a variable, or a function parameter. A str is a pointer in both cases.

The difference between your first and last line: if first line, the pointer is not yet initialized: on what character is points? In second line, a memory is allocated for 16 characters. If this is a local declaration, it is allocated on local stack when a function is executed, if not -- in the process static memory. The pointer is already initialized and point to the first character of the array of 16.


Well, due to harsh arguments on this page, I admit I need to elaborate on the topic "array vs pointer".

Of course I understand that array and pointer are not exactly the same. In the above paragraph I described the difference in memory layout: with array, memory is always taken during initialization (stack or static). There is more to it. Consider the sample:

char arr[16];
	
//will not compile: unknown size:
//char arr2[];
	
for(int index = 0; index < 16; index++)
   arr[index] = index + (int)'0';char* ptr;
ptr = arr;
ptr = &arr[0]; //same thing as before
char third = ptr[3]; //returns '3'
ptr[3] = '?'; //mutable

//makes no sense, compiler does not
//perform static check but it could
//can cause corruption during run-time:
ptr[9000] = '!';
	
//will not compile: "arr is a constant pointer",
//can be initialized, not assigned:
//arr = ptr;



因此,在运行时arr表现为恒定指针,但是它指向的内存区域是可变的(指针arr是恒定的,字符arr[3]不是).没有直接的方法将err修改为指针:它不会编译(示例的最后一行).

在设计期间,存在更多本质差异.特别是,数组长度是静态已知的,等等.



So, during run-time arr behaves as a constant pointer, but the memory area it points to is mutable (pointer arr is constant, character arr[3] is not). There is not direct way to modify err as a pointer: it won''t compile (last line of the sample).

During design time, there are more essential differences. In particular, array length is statically known, etc.


这篇关于当声明char Str [16]时,Str是一个指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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